在使用mod_rewrite重定向后,如何停止重写URL?

时间:2015-03-05 20:39:18

标签: php .htaccess mod-rewrite redirect

在使用mod_rewrite

时,如何在达到第一个成功规则后停止

我尝试过使用LE标记的不同组合,但尚未找到办法执行此操作。

以下是我最新尝试的.htaccess文件

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/(basket|order).html [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,E=STOP:1]
RewriteCond %{ENV:STOP} !1
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(basket|order).html [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L,E=STOP:1]
RewriteCond %{ENV:STOP} !1
RewriteRule ^(.*).html$ cms.php?pagename=$1 [QSA]
RewriteCond %{ENV:STOP} !1
RewriteRule ^$ cms.php [QSA]

例如,每当我尝试打开http://www.example.com/basket.html时,它都会将我重定向到http://www.example.com/cms.php?pagename=basket。但这不是正确的,我只是尝试从http重定向到https

1 个答案:

答案 0 :(得分:1)

请遵守以下规则:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} /(basket|order)\.html [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !/(basket|order)\.html [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L]

RewriteRule ^$ cms.php [L]

RewriteRule ^(.+?)\.html$ cms.php?pagename=$1 [L,QSA,NC]

使用THE_REQUEST代替REQUEST_URI,因为您的最后一条规则是将.html请求转发给cms.php,从而更改了REQUEST_URI的值,而THE_REQUEST仍然存在不变。