即使页面存在,mod_rewrite中的最后一个规则也会返回404

时间:2011-09-25 10:54:45

标签: php mysql apache .htaccess mod-rewrite

我的.htacccess文件中有以下内容:

RewriteBase /
ErrorDocument 404 /404.php
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^news/([a-zA-Z]+)/([0-9]+)-([a-zA-Z]+) /news/view-article.php?category=$1&id=$2&title=$3 [NC]
RewriteRule ^news/most-viewed/([a-zA-Z]+)/([0-9]+)-([a-zA-Z]+) /news/view-article.php?category=$1&id=$2&title=$3 [NC]

RewriteRule ^news/categories/([a-zA-Z]+) /news/categories/view-category.php?category=$1 [NC]

除最后一条规则外,一切都有效。我已经检查过所有页面都存在,新添加的规则遵循与上述规则相同的结构,这些规则很复杂。

变量是否需要改变?我错过了一些代码吗?

解决:

RewriteBase /
ErrorDocument 404 /404.php
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^news/categories/?$ /news/categories.php [NC]
RewriteRule ^news/most-viewed/?$ /news/most-viewed.php [NC]

RewriteRule ^news/categories/([a-zA-Z]+)/?$ /news/view-category.php?category=$1 [NC]


RewriteRule ^news/categories/([a-zA-Z]+)/([0-9]+)-([a-zA-Z]+) /news/view-article.php?category=$1&id=$2&title=$3 [NC]

我最后错过了$。

1 个答案:

答案 0 :(得分:2)

试试这个,让我知道是否有效

RewriteBase /
ErrorDocument 404 /404.php
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^news/([a-zA-Z]+)/([0-9]+)-([a-zA-Z]+)/?$ /news/view-article.php?category=$1&id=$2&title=$3 [NC,L]
RewriteRule ^news/most\-viewed/([a-zA-Z]+)/([0-9]+)-([a-zA-Z]+)/?$ /news/view-article.php?category=$1&id=$2&title=$3 [NC,L]
RewriteRule ^news/categories/([a-zA-Z]+)/?$ /news/categories/view-category.php?category=$1 [NC,L]