RegEx无法提取参数

时间:2014-08-09 06:54:47

标签: php regex apache .htaccess mod-rewrite

拥有以下格式的网址:http://website.com/item/1

这是我的重写规则

RewriteRule ^item/([0-9]+)/ item.php?id=$1 [NC,L]

当我尝试使用$ _GET [' id']来获取ID时,然后回复它我什么都没得到。

是什么导致这种情况?

这是我的.htaccess

RewriteEngine on

RewriteRule ^home index.php  [NC,L]

RewriteRule ^dashboard dashboard.php [NC,L]

RewriteRule ^register register.php [NC,L]

RewriteRule ^login login.php [NC,L]

RewriteRule ^customize customize.php [NC,L]

RewriteRule ^shop shop.php [NC,L]

RewriteRule ^settings settings.php [NC,L]

RewriteRule ^item/([0-9]+)$ item.php?id=$1 [NC,L]

在页面上,我收到以下错误:

Notice: Undefined index: id in /www/greendaddy.pw/myavatar/root/item.php on line 13

Warning: Cannot modify header information - headers already sent by (output started at /www/greendaddy.pw/myavatar/root/item.php:13) in /www/greendaddy.pw/myavatar/root/item.php on line 23

我认为第二个错误是由第一个错误引起的

1 个答案:

答案 0 :(得分:0)

由于启用了选项MultiViews,因此发生了这种情况。将此行放在规则之前将其关闭。您还需要在模式中使用锚点^$

Options -MultiViews

RewriteEngine on

RewriteRule ^home/?$ index.php  [NC,L]

RewriteRule ^dashboard/?$ dashboard.php [NC,L]

RewriteRule ^register/?$ register.php [NC,L]

RewriteRule ^login/?$ login.php [NC,L]

RewriteRule ^customize/?$ customize.php [NC,L]

RewriteRule ^shop/?$ shop.php [NC,L]

RewriteRule ^settings/?$ settings.php [NC,L]

RewriteRule ^item/([0-9]+)$ item.php?id=$1 [NC,L,QSA]