.htaccess - 仅针对某些特定页面将非 www/http 强制为 www/https
问题描述
我想知道是否可以将某些特定页面从非 www/http 强制转换为 www/https,而将其他一些页面保留为非 www/http。
例子
从非 www/http 到 www/https:
http://example.com到https://www.example.com
但这些将保持非 www 和 http:
我试图在 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} 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
推荐阅读
- python - Pandas Dataframe 时间戳索引:本地化和替换小时和分钟
- asp.net-core - IIS:在 .net 框架下托管 .net Core 应用程序
- python - 表达式 r[[2,3],[2,3]] 如何访问 Python 中的数组
- python - 如何使用 python 提取大型 XML 文件的样本?
- c++ - 为什么我需要在主函数中再次设置纹理?
- php - 急切量词之前的负向回溯
- flutter - 使用 dartz 选项
> 如何设置为无 - 3d - ValueError: ('Unrecognized keyword arguments:', dict_keys(['input_shape'])) while loading model
- c# - .Net Core 中 BitArray 的 HashCode
- ios - iOS错误无法加载资源:不支持的URL