Php htaccess清理url
我想将我的url更改为:Php htaccess清理url,php,.htaccess,mod-rewrite,Php,.htaccess,Mod Rewrite,我想将我的url更改为: www.xxxx.com/en/ -> index.php?lang=en www.xxxx.com/news -> index.php?mod=news 我使用此代码,但它不起作用: RewriteCond %{REQUEST_URI} ^/(pe|en|fr|sp|ar)/$ RewriteRule ^([^/]*)/$ index.php?lang=$1 RewriteCond %{REQUEST_URI} !^/(pe|en|fr|sp|ar)
www.xxxx.com/en/ -> index.php?lang=en
www.xxxx.com/news -> index.php?mod=news
我使用此代码,但它不起作用:
RewriteCond %{REQUEST_URI} ^/(pe|en|fr|sp|ar)/$
RewriteRule ^([^/]*)/$ index.php?lang=$1
RewriteCond %{REQUEST_URI} !^/(pe|en|fr|sp|ar)$
RewriteRule ^([^/]*)$ index.php?mod=$1
var\u dump($\u GET)
result:
array(2) { ["mod"]=> string(9) "index.php" ["PHPSESSID"]=> string(32) "e7a5fc683653b7eea47a52dfc64cd687" }
我还使用了htaccess tester(),一切正常(这两条规则都通过了,因此被应用。第一条规则将/en/
重写为/index.php?lang=en
。然后第二条规则通过,并重写为/index.php?mod=index.php
一旦给定规则通过,使用[L]
选项停止处理:
RewriteCond %{REQUEST_URI} ^/(pe|en|fr|sp|ar)/$
RewriteRule ^([^/]*)/$ index.php?lang=$1 [L]
RewriteCond %{REQUEST_URI} !^/(pe|en|fr|sp|ar)$
RewriteRule ^([^/]*)$ index.php?mod=$1 [L]
您正在捕获index.php。忽略现有文件和目录,并使用
[L]
RewriteCond %{REQUEST_URI} ^/(pe|en|fr|sp|ar)/$
RewriteRule ^([^/]*)/$ index.php?lang=$1 [L]
# Don't rewrite index.php or other existing file/dir
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !^/(pe|en|fr|sp|ar)$
RewriteRule ^([^/]*)$ index.php?mod=$1 [L]
这个对我来说很好:
RewriteRule ^(pe|en|fr|sp|ar)/$ index.php?lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!(?:pe|en|fr|sp|ar)/)([^/]*)$ index.php?mod=$1 [L]
[L]
标志RewriteCond
——模式非常简单,不需要有单独的条件(这意味着在我的实现中必须进行2次匹配,而不是1次)%{REQUEST_FILENAME}
检查请求的资源(原始的或已经重写的)是否在真实的文件/文件夹中,然后再重写。这将防止您面临的双重重写RewriteCond
,因为规则并不太复杂(阅读起来更困难,特别是如果你的正则表达式技能不太好,但它可以工作的话)。这也让我的答案与已经提供的有点不同:)@哈米德:真的吗?我通过您提供的htaccess测试仪运行了它,并获得了预期的结果。它在htaccess测试仪上工作,但在我的服务器上不工作!:(@Hamid:规则是否被完全忽略?您可能需要在顶部的
上重新编写引擎。