簡體   English   中英

htaccess重定向到HTTPS,除了幾個網址

[英]htaccess redirect to HTTPS except a few urls

我是htaccess重定向的新手,但是想要做特別的smth - 我不知道什么是推薦的方式,不知道這是否仍然可行。

我在.htaccess文件中有這個:

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

現在每個URL都被重定向到HTTPS版本 - 這很好並且必要。 但現在有一些例外。

例如,這些URL必須是HTTP而不是HTTPS:

http://www.mywebsite.com/another/url/which/has/to/be/http
http://www.mywebsite.com/and_again?var=a

是否有可能使用htaccess來解決這個問題,當它可能時,您可以發送參考鏈接或描述如何執行此操作。

編輯

我現在有這個代碼:

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} off 
RewriteCond %{THE_REQUEST} !\s/+(/commerce_paypal/*)\s [NC] 
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

目標是每個 (!)url都被重定向到HTTPS,除了任何在開頭都有commerce_paypal的url。

例如:

mydomain.com/commerce_paypal  <- http
mydomain.com/commerce_paypal/smth/else <- http
mydomain.com/what/ever <- https

您可以使用RewriteCondhttp->http規則中添加例外:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# force https:// for all except some selected URLs    
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/commerce_paypal/ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# force http:// for selected URLs
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /commerce_paypal/ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

參考: Apache mod_rewrite簡介

Apache mod_rewrite技術細節

RewriteCond %{THE_REQUEST} !/commerce_paypal/ [NC]

為我工作。 沒有運氣,我嘗試了很多類似的條件重寫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM