mod_rewrite与404冲突的友好URL

时间:2012-08-23 15:12:09

标签: php .htaccess mod-rewrite http-status-code-404 http-status-code-301

我一直在和我的mod_rewrite挣扎一段时间,我以为我已经破解了它......

我的CMS使用post.php生成博客文章网址?s =我一直在尝试删除这部分网址,以便它们很好用户友好。这是我的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,这是一个不存在的网址http://tempertemper.net/this-url-does-not-exist

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

谢谢你看一下:)

1 个答案:

答案 0 :(得分:0)

因为您要重定向符合[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文件中编写新的异常。