簡體   English   中英

這些重寫規則有什么問題? 重定向到https的一頁,返回其他所有的http

[英]What is wrong with these rewrite rules? Redirect to https for one page, back to http for all others

所需行為:

參觀http://www.example.com/our-services/individual-medical-dental-insurance重定向到https: https://www.example.com/our-services/individual-medical-dental-insurance

通過https訪問任何其他頁面 ,例如https://www.example.com/about-group-benefit-services/ ,重定向到非https: http://www.example.com/about-group-benefit-services/ : http://www.example.com/about-group-benefit-services/

我嘗試了各種解決方案,這是我的最新版本(不起作用):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} our-services/individual-medical-dental-insurance
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^our-services\/individual\-medical\-dental\-insurance
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/$1 
[L,R=301]   
</IfModule>

可能值得注意的是,該文件位於WordPress網站中,.htaccess文件的其余部分包含以下內容:

# 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

(.htaccess除以上內容外不包含其他任何內容)。

不知道這是否能使它正常工作,但是您的規則存在一些問題,因此不適合發表評論:

%{REQUEST_URI}始終以“ /”開頭,因此您的正則表達式將始終為true(因為它永遠不會以“ our-services”開頭)。

另一件事是您的規則由於$1反向引用而在末尾附加了URL,因此您需要刪除它。

最后,兩個重定向都將轉到https://

另外,您無需轉義斜杠或破折號:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/our-services/individual-medical-dental-insurance
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/our-services/individual-medical-dental-insurance
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]   
</IfModule>

暫無
暫無

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

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