0

我一直在为我的 mod_rewrite 苦苦挣扎,我以为我已经破解了它……</p>

我的 CMS 使用 post.php?s= 生成博客文章 URL,我一直在尝试删除 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 ,这是一个不存在的 URL http://tempertemper.net/this-url-does-not-exist

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

谢谢参观 :)

4

1 回答 1

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 文件中编写新的异常。

于 2012-08-24T18:22:12.633 回答