【发布时间】:2012-05-06 18:27:25
【问题描述】:
我有一个前端控制器执行以下操作:
$pages = array('home', 'login', 'create-account', 'activate');
$page = ((empty($_GET['p'])) ? 'home' : $_GET['p']);
if (in_array($page, $pages) && file_exists($page . '.php'))
include $page . '.php';
else
include '404.php';
然后我有这个mod_rewrite
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z-]+)$ index.php?p=$1 [L]
RewriteRule ^(activate)/([\w-]+)$ index.php?p=$1&f=$2
如果我去
有效:
如果我访问:
它会给我一个真正的 404(不是我的自定义 :P),而不是把我送到 404 页面。这是因为重写规则:
RewriteRule ^([a-z-]+)$ index.php?p=$1 [L] 因为它只接受 a-z 和 -
如果我把它改写成:
RewriteRule ^(.*)$ index.php?p=$1 [L]
它可以工作并发送到 404,但是这种重写不起作用:
RewriteRule ^(activate)/([\w-]+)$ index.php?p=$1&f=$2
因为第一个会匹配。
如果我想换个地方:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(activate)/([\w-]+)$ index.php?p=$1&f=$2 [L]
RewriteRule ^(.*)$ index.php?p=$1 [L]
我在所有页面上都收到 404。
我该如何解决这个问题?
【问题讨论】:
-
您的重写规则似乎只查找小写字母和破折号?
-
请阅读整个问题。
-
您可能想尝试
RewriteRule ^([^/]*)$ index.php?p=$1,它将排除任何带有 / 的字符串(因此您的第一个字符串将再次起作用。)但这会导致其中带有 /s 的垃圾无法获取啜饮。 -
我不希望 /s 出现任何问题或垃圾,还有其他方法可以解决吗?
-
除非没有匹配的重写,否则不能将用户指向某个 url (404)
标签: php mod-rewrite