仅针对某些特定页面强制将非 www/http 强制为 www/https



我想知道是否有可能强制某些特定页面从非www/http到www/https,并保留其他一些非www/http的页面。

从非 www/http 到 www/https:

http://example.com 到 https://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。

但它似乎效果不佳

试试这些:

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^folder1.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^folder2.*$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^folder1.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^folder2.*$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^folder1.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

等等...

试试这个:

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

相关内容

  • 没有找到相关文章

最新更新