简体   繁体   English

需要针对子域URL的htaccess重定向代码解决方案,以重定向到没有www的URL

[英]Need htaccess redirect code solution for subdomain URL to redirect to URL without www

I have a domain example.com and I have added the following redirect code in .htaccess file to redirect to https://www.example.com/ , https://test.example.com/ and https://www.example.com/test/ 我有一个域example.com和我已经添加在.htaccess中重定向代码重定向到https://www.example.com/https://test.example.com/https://开头WWW。 example.com/test/

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

1) When I load the URL in the browser example.com, it is redirected to https://www.example.com 1)当我在浏览器example.com中加载URL时,它被重定向到https://www.example.com

2) I have created a subdomain folder 'test' in example.com. 2)我在example.com中创建了一个子域文件夹'test'。 So when I load the URL example.com/test, it is redirected to https://www.example.com/test/ 因此,当我加载URL example.com/test时,它会被重定向到https://www.example.com/test/

Need solution for the following : 需要以下解决方案

1) When I load the URL test.example.com, it is redirected to https://www.test.example.com but I want it to be redirected to https://test.example.com 1)当我加载URL test.example.com时,它被重定向到https://www.test.example.com但我希望它被重定向到https://test.example.com

2) When I load the URL www.example.com/test, it is redirected to https://www.www.example.com/test/ but I want it to be redirected to https://www.example.com/test/ 2)当我加载URL www.example.com/test时,它会被重定向到https://www.www.example.com/test/但我希望它被重定向到https://www.example.com /测试/

3) When I load the URL www.example.com, it is redirected to https://www.www.example.com/ but I want it to be redirected to https://www.example.com/ 3)当我加载URL www.example.com时,它被重定向到https://www.www.example.com/但我希望它被重定向到https://www.example.com/

Can anyone please help? 有人可以帮忙吗? Thanks in advance 提前致谢

You can use this rule 您可以使用此规则

RewriteEngine on

#redirect subdomain to https
RewriteCond %{HTTPS}#%{HTTP_HOST} ^off#(sub\.domain\.com)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]
#redirect main domain to https www
RewriteCond %{HTTPS}#%{HTTP_HOST} ^off#(?:www\.)?(example\.com)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]

Clear your browser cache before testing these new redirects. 在测试这些新重定向之前清除浏览器缓存。

If you have multiple subdomains , you can use the following pattern to redirect them to https : 如果您有多个子域,则可以使用以下模式将其重定向到https:

^off#(sub\.domain\.com|sub2.example.com|sub3.example.com|other)$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM