mod_rewrite友好URL与404冲突


mod_rewrite Friendly URL clashing with 404

我已经在mod_rewrite上挣扎了一段时间,我以为我已经破解了它…

我的CMS使用post.php生成博客文章URL?s=,我一直在尝试删除URL的这一部分,这样它们就很好,对用户友好。这是我的mod_rewrite:

<ifModule mod_rewrite.c>
DirectoryIndex index.php index.html
ErrorDocument 404 http://tempertemper.net/error.php
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^www.tempertemper'.net$
RewriteCond %{REQUEST_URI} !cron.php
RewriteRule ^(.*)$ http://tempertemper.net/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^s=(.+)$
RewriteRule post.php /%1? [R=301,L]
RewriteRule ^resources /archive? [R=301,L]
RewriteCond %{REQUEST_URI} !post.php
RewriteRule ^([a-zA-Z0-9_-]+)$ post.php?s=$1
</ifModule>

不幸的是,它似乎将所有找不到的页面都指向空白的post.php页面,而不是error.php页面。

我的网站在这里:http://tempertemper.net这是一个不存在的URLhttp://tempertemper.net/this-url-does-not-exist

它应该带你去http://tempertemper.net/error

感谢您观看:)

因为您正在重定向符合[a-zA-Z0-9_-]+模式的所有路径,所以这只需向您的post.php脚本发送查询字符串s=this-url-does-not-exist。因此,由您的PHP脚本post.PHP来检查this-is-not-a-url是否存在,因为Apache不可能知道哪些博客文章ID值有效,哪些无效。

顺便说一句,我建议使用/blog/2012-08-24-whatever作为给访问者的路径,而不是/2012-08-24-whatever。这样,您就可以为其他函数(如/images)保留路径,而不必每次需要创建新的实用程序路径时都在.htaccess文件中写入新的异常。