.htaccess - 重写规则被忽略或无效

时间:2013-08-02 13:32:03

标签: apache .htaccess mod-rewrite

这是我的测试.htaccess

    RewriteEngine On
    Options +FollowSymLinks    

    #WWW redirect WORKS
    RewriteCond %{HTTP_HOST} !^$
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    RewriteRule ^home/$ index.php [L] #THIS RULE  WORKS
    RewriteRule ^news/(.+)/$ /news.php?page=$1 [L] #not works
    RewriteRule ^news/(.+)/$ /news.php?article=$1 [L] #not works
    RewriteRule ^news/$ news.php [L] #WORKS

我想这样做:

"news.php" => "news/"    
"news.php?page=3" => "news/3/"
"news.php?article=lorem-ipsum-3" => "news/lorem-ipsum-3/"

使用此网址规则,这样的网址效果很好,但不是我想要的:

http://www.xxxxxx.com/news/?page=3  #works
http://www.xxxxxx.com/news/?article=lorem-ipsum-2 #works

如何修复.htaccess?

感谢

1 个答案:

答案 0 :(得分:1)

我看到的唯一问题是你有两个匹配相同条件的规则(L标志阻止它检查更多规则)并且你在重写路径前面有一个额外的/

RewriteRule ^news/(.+)/$ news.php?page=$1 [L]
RewriteRule ^news/(.+)/$ news.php?article=$1 [L] # Will never execute

除此之外,它们看起来很好。 (我通过htaccess tester运行它们,它们以正确的规则终止。

如果您没有提供明确的规范链接,并且您没有使用.htaccess在最后手动添加/(如果缺少),您可能需要考虑进行最终{{1可选/

?

<强>更新 假设它是一个页面,如果它只是一个数字,而一篇文章,如果它是其他任何东西,那么:

RewriteRule ^news/(.+)/?$ news.php?page=$1 [L]