regex - 非 https 主站点重定向到带有目录的 https
问题描述
我正在使用这段代码将我的 .htaccess 文件中的所有 http 重定向到 https。
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
效果很好。但我唯一的问题是如果我访问 root 非 http
http://example.com redirects to https://www.example.com/dev/
/dev 是主站点所在的目录。
我的cpanel目录是这样的
public_html/dev <---- https://www.example.com
我该如何解决这个问题。它应该重定向到没有 dev 目录的 https 版本。希望有人可以阐明这一点。谢谢。
public_html 中 .htaccess 的内容是
public_html/.htaccess
rewritecond %{HTTP_HOST} ^example.com$ [NC,OR]
rewritecond %{HTTP_HOST} ^www.example.com$
rewritecond %{REQUEST_URI} !dev/
rewriterule (.*) /dev/$1 [L]
RewriteCond %{HTTP_HOST} ^oldurl\.com$ [OR] // if old url redirect to example.com
RewriteCond %{HTTP_HOST} ^www\.oldurl\.com$ // if old url redirect to example.com
RewriteRule ^/?$ "https\:\/\/www\.example\.com" [R=301,L]
这是 public_html/dev 中 .htaccess 文件的内容
public_html/dev/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
解决方案
就这样吧。
public_html/.htaccess
RewriteCond %{HTTP_HOST} ^(?:www\.)?oldurl\.com$ [NC]
RewriteRule ^/?$ https://www\.example\.com [R=301,L]
# enforce HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# First add a trailing slash if dev/$1 is a directory
RewriteCond %{DOCUMENT_ROOT}/dev/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=301,NE]
# forward everything to /dev directory
RewriteCond %{REQUEST_URI} !^/dev/ [NC]
RewriteRule .* dev/$0 [L]
public_html/dev/.htaccess:
RewriteEngine On
RewriteBase /dev/
RewriteRule ^index\.php$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
确保在测试此更改之前完全清除浏览器缓存。
推荐阅读
- python - 如何运行 gRPC 客户端来测试 tensorflow 模型?
- c# - C# 存储过程函数需要未提供的参数“@id”
- php - 数组未显示来自内爆结果的匹配项
- hadoop - 在 HIVE 表中生成唯一的 bigint 代理键
- oracle-apex - oracle apex 中的柱形图
- for-loop - 如何为回归循环编写本地宏
- javascript - Javascript 正则表达式替换捕获
- java - Eclipse / IntelliJ IDEA 上的 Maven 设置
- python - Python3 http.sever 无效语法
- java - 有没有办法使用 java 客户端从 Amqp 0.9.1 RabbitMq 代理获取绑定到队列的所有标头?