在相似的 URL 中重写Cond

发布于 2024-12-15 04:34:14 字数 3325 浏览 0 评论 0原文

我有一个 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。因此,您永远不能位于篮子/$和篮子/删除/中。所以我很困惑如何做到这一点......

谢谢

I've got an HTACCESS which contols HTTPS/HTTP redirection based on url, most of it is easy to do except for a specific part.

The basket is dual protocol, some parts of it is HTTP and others is HTTPS namely the part where you enter your shipping and billing address and the part where you enter your credit card information. But the rest, such as /basket/, /basket/delete/, /basket/add/, /basket/modify/ should be HTTP.

I was able to do the the HTTP => HTTPS part but the reverse is quite harder because RewriteCond seems to be an AND, and i need OR...

At the same time, if you guys can help me make it more compact and readable, i'd be really happy, mod_rewrite is not my best tool :)

Right now it looks like this: (Note i know some rules seem to repeat but we have issues with the urls, so i need 2 versions most of the time for the basket part)

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

I was thinking of adding something like this:

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]

But this script will obviously will not work because RewriteCond is based on AND and not OR. So you can never be in basket/$ and basket/delete/ for example. So i'm stumped as to how to do this...

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

债姬 2024-12-22 04:34:14

第一:使用 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]

我最喜欢的检查正则表达式的工具:

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]

First: with RewriteCond you can use [OR] directives to reach you goal.

But you can write much shorter RewriteCond:


Your last request should be like this:

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]

My favorite tool to check for regexp:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)


Optimization

Your whole rules for the port 80 should be very close to:

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]

Your rules for the port 443 should be very close to:

RewriteCond %{SERVER_PORT} 443
RewriteRule ^(produits|products)/.*$ http://%{SERVER_NAME}%{REQUEST_URI} [R,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文