清除wordpress中自定义$ _GET变量的URL

时间:2012-04-15 10:45:25

标签: php wordpress .htaccess mod-rewrite

我正在使用wordpress非常永久链接,我的.htaccess文件看起来像这样:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /nafham/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /nafham/index.php [L]
</IfModule>

# END WordPress

我正在使用一个显示带有url变量的索引页面的表单,如下所示:

http://www.nafham.com/?edu_year_selectionreal=79&semester_selectionreal=15&subject_selectionreal=80

我想让网址显示如下:

http://www.nafham.com/79/15/80

有可能这样做吗?为了不使重写条件出现问题,最好的方法是什么?wordpress已经应用了吗?

3 个答案:

答案 0 :(得分:3)

因为您已经设置了重写,所以您需要做的就是解析index.php中的url

if (preg_match("#(\d+)/(\d+)/(\d+)#", $_SERVER['REQUEST_URI'], $regs)) {

    $edu_year_selection_real = $regs[1]
    $semester_selectionreal = $regs[2];
    $subject_selectionreal = $regs[3];
}

答案 1 :(得分:0)

添加smth。像这样的.htaccess:

RewriteRule ^/([0-9-]+)/([0-9-]+)/([0-9-]+)$ index.php?var1=$1&var2=($2)&var3=($3) [L]

答案 2 :(得分:0)

您需要在Wordpress中设置自定义重写规则,否则Wordpress会将您转到错误页面。

以下是设置自定义重写规则的示例:

      <?php

  add_filter('rewrite_rules_array', 'insert_custom_rules');
  function insert_custom_rules($rules)
  {
$newrules = array();
$newrules['(.+?)/(.+?)/(.+?)/?$'] =
'index.php?edu_year_selectionreal=$matches[1]&semester_selectionreal=$matches[2]&subject_selectionreal=$matches[3]';
return $newrules + $rules;
  }
  ?>

希望这会有所帮助。