RewriteCond里面有类似的URL

时间:2011-11-14 15:38:15

标签: php .htaccess mod-rewrite

我有一个HTACCESS,它基于url控制HTTPS / HTTP重定向,除了特定部分之外,大部分都很容易做到。

购物篮是双协议,其中一部分是HTTP,其他部分是HTTPS,即您输入运费和帐单地址的部分以及您输入信用卡信息的部分。但其余的,比如/ basket /,/ basket / delete /,/ basket / add /,/ basket / modify /应该是HTTP。

我能够做HTTP => HTTPS部分,但反过来相当困难,因为RewriteCond似乎是一个AND,我需要OR ...

与此同时,如果你们可以帮助我让它更紧凑和可读,我会非常高兴,mod_rewrite不是我最好的工具:)

现在它看起来像这样:(注意我知道一些规则似乎重复,但我们的网址有问题,所以我大部分时间需要2个版本的篮子部分)

Options +FollowSymLinks

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{SERVER_PORT} 80
RewriteRule ^admin/.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !basket/$
RewriteCond %{REQUEST_URI} !basket/add/
RewriteCond %{REQUEST_URI} !basket/delete/
RewriteCond %{REQUEST_URI} !basket/modify/
RewriteCond %{REQUEST_URI} !basket//add/
RewriteCond %{REQUEST_URI} !basket//delete/
RewriteCond %{REQUEST_URI} !basket//modify/
RewriteCond %{REQUEST_URI} !basket/info_panier.js
RewriteRule ^basket/.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !panier/$
RewriteCond %{REQUEST_URI} !panier/add/
RewriteCond %{REQUEST_URI} !panier/delete/
RewriteCond %{REQUEST_URI} !panier/modify/
RewriteCond %{REQUEST_URI} !panier//add/
RewriteCond %{REQUEST_URI} !panier//delete/
RewriteCond %{REQUEST_URI} !panier//modify/
RewriteCond %{REQUEST_URI} !panier/info_panier.js
RewriteRule ^panier/.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteCond %{SERVER_PORT} 80
RewriteRule ^utilisateurs/.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteCond %{SERVER_PORT} 80
RewriteRule ^users/.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteCond %{SERVER_PORT} 443
RewriteRule ^produits/.*$ http://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteCond %{SERVER_PORT} 443
RewriteRule ^products/.*$ http://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteRule ^produits/ /catalogue_files/default/fr.php [L]
RewriteRule ^products/ /catalogue_files/default/en.php [L]

RewriteRule ^panier/ /panier_files/default/fr.php [L]
RewriteRule ^basket/ /panier_files/default/en.php [L]

RewriteRule ^utilisateurs/ /utilisateurs_files/default/fr.php [L]
RewriteRule ^users/ /utilisateurs_files/default/en.php [L]

</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

# END WordPress

我想添加这样的东西:

RewriteCond %{SERVER_PORT} 443
RewriteCond %{REQUEST_URI} !admin/
RewriteCond %{REQUEST_URI} !users/
RewriteCond %{REQUEST_URI} !utilisateurs/
RewriteCond %{REQUEST_URI} basket/$
RewriteCond %{REQUEST_URI} basket/delete/
RewriteCond %{REQUEST_URI} basket/modify/
RewriteCond %{REQUEST_URI} basket//add/
RewriteCond %{REQUEST_URI} basket//delete/
RewriteCond %{REQUEST_URI} basket//modify/
RewriteCond %{REQUEST_URI} basket/info_panier.js
RewriteCond %{REQUEST_URI} produits/
RewriteCond %{REQUEST_URI} products/
RewriteCond %{REQUEST_URI} pdv/
RewriteCond %{REQUEST_URI} pos/
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI} [R,L]

但是这个脚本显然不会起作用,因为RewriteCond基于AND而不是OR。所以你永远不能在篮子/ $和篮子/删除/例如。所以我很难过如何做到这一点......

由于

1 个答案:

答案 0 :(得分:1)

首先:使用RewriteCond,您可以使用[OR]指令达到目标。

但你可以写更短的RewriteCond:


您的上一次请求应该是这样的:

RewriteCond %{SERVER_PORT} 443
RewriteCond %{REQUEST_URI} !(admin|users|utilisateurs)/
RewriteCond %{REQUEST_URI} ^(((panier|basket)[/]{0,2}(add/|delete/|modify/|info_panier.js)?)|produits/|products/|pdv/|pos/)$
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI} [R,L]

我最喜欢检查regexp的工具:

http://www.quanetic.com/Regex(别忘了选择ereg(POSIX)而不是preg(PCRE)!)


优化

端口80的整体规则应非常接近:

RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !((panier|basket)/(/)?(add/|delete/|modify/|info_panier.js))
RewriteRule ^(admin|basket|panier|utilisateurs|users)/.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

您对端口443的规则应非常接近:

RewriteCond %{SERVER_PORT} 443
RewriteRule ^(produits|products)/.*$ http://%{SERVER_NAME}%{REQUEST_URI} [R,L]