我有一个基于PHP的Web应用程序,我正在尝试应用Apache的mod_rewrite。
原始URL的格式为: http://example.com/index.php?page=home&x=5
我想把这些转化为: http://example.com/home?x=5
请注意,在重写页面名称时,我也有效地“移动”了问号。当我尝试这样做时,Apache很高兴地执行以下翻译:
RewriteRule ^/([a-z]+)\?(.+)$ /index.php?page=$1&$2 [NC,L]
但它把 $_GET PHP中的变量。例如,调用 http://example.com/home?x=88 收益率只有一 $GET 变量(变量) page => home )哪里做的 x => 88 去吧?但是,当我将规则更改为使用与号而不是问号时:
$_GET
http://example.com/home?x=88
$GET
page => home
x => 88
RewriteRule ^/([a-z]+)&(.+)$ /index.php?page=$1&$2 [NC,L]
像一个电话 http://example.com/home&x=88 将按我的预期工作(即 page 和 x $_get variables are set appropriate).
http://example.com/home&x=88
page
x
我知道差异很小,但是如果可能的话,我希望我的URL变量以问号“开始”。我确信这反映了我自己对mod重写重定向如何与php交互的误解,但我似乎能够做到这一点(无论是以何种方式)。
事先谢谢! 干杯, -克里斯
试试这个。
RewriteCond %{REQUEST_URI} !^/index.php RewriteRule ^/([a-z]+)(?:$|\?(?:.+)) /index.php?page=$1 [NC,L,B,QSA,NE]
B对backreference进行转义(不必这样做,因为它与[A-Z]+匹配,但是如果您希望稍后扩展它,它可能会很有用)。
编辑:添加了重写秒。 编辑2:qsa负责添加和号。