首页 > 解决方案 > 仅针对某些特定页面将非 www/http 强制为 www/https

问题描述

我想知道是否可以将某些特定页面从非 www/http 强制转换为 www/https,而将其他一些页面保留为非 www/http。

例子

从非 www/http 到 www/https:

http://example.comhttps://www.example.com

但这些将保持非 www 和 http:

http://example.com/folder1/ *

http://example.com/folder2/ *

我试图在 htaccess 文件中添加这个规则条件:

RewriteEngine On     

# Enable HTTPS and WWW for homepage
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{HTTPS} off
RewriteRule ^$ https://example.com/ [R=301,L]

# Disable HTTPS and WWW for all pages
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{HTTPS} on
RewriteRule . http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

然后它应该强制 https 和 www 只是主页并将/folder1//folder2/等其他页面留给htpp 非 www。

但是好像效果不好

标签: .htaccessmod-rewritehttp-redirect

解决方案


尝试这个:

RewriteEngine  On
RewriteCond    %{HTTPS} off       ## http requests only
RewriteRule    ^folder1/  - [L]   ## stop for folder1/ 
RewriteRule    ^folder2/  - [L]   ## stop for folder2/
                                  ## then redirect all other requests:
RewriteRule    ^(.*)$     https://www.example.com%{REQUEST_URI} [END,NE,R=permanent] 

同样,您可以正则表达式您的跳过:

RewriteRule    ^folder(\d+)/  - [L]   ## stop redirection for any folder[number]/

或将其设为负数RewriteCond,保持单一RewriteRule

RewriteEngine  On
RewriteCond    %{HTTPS} off                        ## http requests only
RewriteCond    %{REQUEST_URI}   !^/folder(\d+)/    ## skip any folder[number]/
RewriteRule    ^(.*)$     https://www.example.com%{REQUEST_URI} [END,NE,R=permanent] 

等等...完整文档:https ://httpd.apache.org/docs/2.4/rewrite/flags.html


推荐阅读