简体   繁体   English

RewriteRule .htaccess无效

[英]RewriteRule .htaccess not working

I am facing problem with mod_rewrite ^(.+)$ /article.php?pname=$1 [L,QSA] is working fine but after that other RewriteRule dont work i dont know why ... here is my code 我面临mod_rewrite ^(.+)$ /article.php?pname=$1 [L,QSA]问题mod_rewrite ^(.+)$ /article.php?pname=$1 [L,QSA]工作正常,但之后其他RewriteRule不工作我不知道为什么...这里是我的代码

   <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
        RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.+)$ /article.php?pname=$1 [L,QSA]
        RewriteRule ^category/(.*)$ /cat.php?name=$1 [QSA,L]
        ErrorDocument 404 /index.php
    </IfModule>

If i remove RewriteRule ^(.+)$ /article.php?pname=$1 [L,QSA] other RewriteRule works but it dont work with this, how can both work together ? 如果我删除RewriteRule ^(.+)$ /article.php?pname=$1 [L,QSA]其他RewriteRule可以工作,但它不能用于此,它们如何一起工作? Please help 请帮忙

Keep your .htaccess like this: 保持你的.htaccess像这样:

ErrorDocument 404 /index.php
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]

    RewriteRule ^category/(.*)$ cat.php?name=$1 [QSA,L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.+)$ article.php?pname=$1 [L,QSA]
 </IfModule>

Simple change order, from: 简单的变更单,来自:

RewriteRule ^(.+)$ /article.php?pname=$1 [L,QSA]
RewriteRule ^category/(.*)$ /cat.php?name=$1 [QSA,L]

To: 至:

RewriteRule ^category/(.*)$ /cat.php?name=$1 [QSA,L]
RewriteRule ^(.+)$ /article.php?pname=$1 [L,QSA]

Because (.+) will rewrite everithing including cat.php . 因为(.+)将重写包括cat.php在内的cat.php So when you are trying to access cat.php?name=someName you will get /article.php?pname=cat.php%3Fname%3DsomeName . 因此,当您尝试访问cat.php?name=someName您将获得/article.php?pname=cat.php%3Fname%3DsomeName

You should do following: 你应该做以下事情:

RewriteCond $1 !^(cat\.php|someFolder|robots\.txt)
RewriteRule ^(.+)$ /article.php?pname=$1 [L,QSA]

Or even this: 甚至这个:

RewriteCond %{DOCUMENT_ROOT} !-f
RewriteRule !(cat\.php|someFolder|robots\.txt)$ /article.php?pname=$1 [QSA,L]

robots.txt should not to be rewrited. robots.txt不应该重写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM