覆盖$ _GET变量的url重写

时间:2012-04-12 11:18:30

标签: .htaccess mod-rewrite get

我的管理系统中有一个页面,其中包含以下路径:

  

http://192.168.1.253/xxxxxx/admin/view/news-items/issue/3

我正在尝试重写它,以便它指向:

  

http://192.168.1.253/xxxxxx/admin/view/news-items.php?issue=3

但是我似乎无法使它工作,因为页面显示但是php无法检测$ _GET变量。我有2个.htaccess文件,一个在xxxxxx目录中,另一个在admin目录中。他们在这里:

XXXXXX:

RewriteEngine On    # Turn on the rewriting engine

RewriteRule  ^([^/.]+)/delete/(1-9)/?$ $1.php?delete=$2 [L]
RewriteRule  ^([^/.]+)/?$ $1.php [L] # rewrite without query string parameters
RewriteRule  ^([^/.]+)/([^/.]+)/?$ $1.php\#$2 [L] # one name string parameter

管理员:

RewriteEngine On    # Turn on the rewriting engine

RewriteRule  ^([^/.]+)/?$ $1.php [L] # rewrite without query string parameters
RewriteRule  ^([^/.]+)/(\d+)/?$ $1.php?id=$2 [L] # one id parameter
RewriteRule  ^([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)/issue/(\d+)/?$ $1.php?issue=$2 [L] # one issue parameter
RewriteRule  ^([^/.]+)/(\d+)/(\d+)/?$ $1.php?id=$2&file_id=$3 [L] # two parameters

修改

这是新代码:

XXXXXX:

RewriteEngine On    # Turn on the rewriting engine

#RewriteRule  ^([^/.]+)/delete/(\d+)/?$ $1.php?delete=$2 [L,QSA]
RewriteRule  ^([^/.]+)/?$ $1.php [L,QSA] # rewrite without query string parameters
RewriteRule  ^([^/.]+)/([^/.]+)/?$ $1.php\#$2 [L,QSA] # one name string parameter

管理员:

RewriteEngine On    # Turn on the rewriting engine

#RewriteRule  ^([^/.]+)/?$ $1.php [L,QSA] # rewrite without query string parameters
#RewriteRule  ^([^/.]+)/(\d+)/?$ $1.php?id=$2 [L,QSA] # one id parameter
RewriteRule  ^([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)/issue/(\d+)/?$ $1.php?issue=$2 [L,QSA] # one issue parameter
RewriteRule  ^([^/.]+)/(\d+)/(\d+)/?$ $1.php?id=$2&file_id=$3 [L,QSA] # two parameters

1 个答案:

答案 0 :(得分:1)

您需要在重写规则中包含[QSA]标记:

RewriteRule  ^([^/.]+)/delete/(1-9)/?$ $1.php?delete=$2 [L,QSA]
RewriteRule  ^([^/.]+)/?$ $1.php [L,QSA] # rewrite without query string parameters
# etc.

来自documentation

  

修改查询字符串

     

默认情况下,查询字符串不会更改。但是,您可以在包含查询字符串部分的替换字符串中创建URL。只需在替换字符串中使用问号,即表示应将以下文本重新注入查询字符串。如果要删除现有查询字符串,请仅使用问号结束替换字符串。要合并新旧查询字符串,请使用[QSA]标记。