Ryf*_*lex 3 regex apache .htaccess mod-rewrite
我正在尝试制定一个 .htaccess 规则,我可以在我的主域和子域上使用它,而无需更改每个域的代码。
我已经尝试了以下子域来尝试强制使用 https 和非 www,但它无法正常工作。
<IfModule mod_rewrite.c>
# Force HTTPS & NON-WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.test\.website\.com$ [NC] # Detect if it has www?
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^test\.website\.com$ [NC]
RewriteRule ^ https://test\.website\.com%{REQUEST_URI} [R=301,L]
</IfModule>
Run Code Online (Sandbox Code Playgroud)
目前,如果我要去http://www.test.website.com/test/它重定向到https://test%2Cwebsite.com/所以它有点工作但不完全。
我一直在尝试使用以下方法更动态地执行此操作:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on [NC]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</IfModule>
Run Code Online (Sandbox Code Playgroud)
但这根本行不通。实现这一目标的最佳解决方案是什么?
它基本上应该强制执行以下所有操作:
http://website.com/ => https://website.com/
http://www.website.com/ => https://website.com/
https://www.website.com/ => https://website.com/
http://website.com/testing/ => https://website.com/testing/
http://www.website.com/testing/ => https://website.com/testing/
https://www.website.com/testing/ => https://website.com/testing/
http://test.website.com/ => https://test.website.com/
http://www.test.website.com/ => https://test.website.com/
https://www.test.website.com/ => https://test.website.com/
http://test.website.com/testing/ => https://test.website.com/testing/
http://www.test.website.com/testing/ => https://test.website.com/testing/
https://www.test.website.com/testing/ => https://test.website.com/testing/
Run Code Online (Sandbox Code Playgroud)
您可以使用这个单一的动态规则来实现所有http -> https和www -> non-www重定向:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
Run Code Online (Sandbox Code Playgroud)
确保此规则位于顶部,并在测试此更改之前清除浏览器缓存。
| 归档时间: |
|
| 查看次数: |
1255 次 |
| 最近记录: |