apache - htaccess 更改域名,但前缀“www”除外。
问题描述
我想通过 htaccess 为我的域“example.com”实现以下目标:
- 始终在域名前添加“https://”或将“http://”更改为“https://”;
- 添加前缀“www。” 如果没有使用前缀,则在域名之前(“https://”之后),并且不要更改其他前缀(如“mobile.”)。
所以“example.com”或“http://example.com”将变为:“https://www.example.com”和“mobile.example.com”或“http://mobile.example.com” ' 将变为 'https://mobile.example.com'。
解决方案
你的.htaccess
文件应该是:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Start with www
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
#RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
# force http to https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
推荐阅读
- oracle - SQL Developer 未因存储过程错误而失败
- jquery - 按经纬度过滤
- python - ValueError:lstm_45 层的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。收到的完整形状:(无,无,无,128)
- node.js - NPM 模块中的 bash 脚本如何访问其 NPM 环境变量?
- python - tf.signal.stft 返回 -inf
- typescript - 勇敢的浏览器卡住了,当我访问 /register 路由时,它会将我重定向到动态路由
- javascript - 访问道具时未获得正确的数据
- python - 在数据框中查找浮点值并按列范围替换
- python - 嗨,我正在尝试在 VScode 中导入/安装请求和漂亮的汤,但以下不起作用
- python - 多索引数据框熊猫中负值的括号