[英]Force HTTPS & Non-WWW for a subdomain dynamically
I'm trying to work out a .htaccess rule that I can use on my main domain & subdomains without requiring changes to the code for each one. 我正在尝试制定一个.htaccess规则,我可以在我的主域和子域上使用,而无需更改每个规则的代码。
I've tried the following for a subdomain to try and force https and non-www but it doesn't work correctly. 我已尝试以下子域尝试强制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>
Currently if I was to go to http://www.test.website.com/test/
it redirects to https://test%2Cwebsite.com/
so it sort of works but not quite. 目前,如果我要访问http://www.test.website.com/test/
它会重定向到https://test%2Cwebsite.com/
所以它有点工作但不完全。
I've been trying to do it more dynamically too using: 我一直在尝试使用以下方式更动态地执行此操作:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on [NC]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</IfModule>
But this isn't working at all. 但这根本不起作用。 What's the best solution to achieve this? 实现这一目标的最佳解决方案是什么?
It should essentially be forcing all of the following: 它应该基本上强制执行以下所有操作:
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/
You can use this single dynamic rule to achieve all http -> https
and www -> non-www
redirections: 您可以使用此单一动态规则来实现所有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]
Make sure this rule is right at top and you clear your browser cache before testing this change. 确保此规则位于顶部,并在测试此更改之前清除浏览器缓存。
Your rewrite rule must be (in the first example): 您的重写规则必须是(在第一个示例中):
RewriteRule ^/(.*) https://test.website.com/$1 [R=301,L]
Remove the backslashes, as the rewrite goal is no regex. 删除反斜杠,因为重写目标不是正则表达式。
For your overall solution, I guess you need two seperate rewrites. 对于您的整体解决方案,我猜您需要两次单独的重写。 One to do the https check and the other to remove the www. 一个用于执行https检查,另一个用于删除www。 I guess it will not be possible in one rewrite, because you need to check two conditions, of which only one might match. 我想在一次重写中是不可能的,因为你需要检查两个条件,其中只有一个可能匹配。
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^/(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^/(.*)$ https://%1/$1 [R=301,L]
Try this: 试试这个:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# First, force the HTTPS, whatever it is:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Then drop the www, if any:
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule .* https://%1%{REQUEST_URI} [L,R=301]
</IfModule>
## Results:
# 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/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.