问题描述
我正在为以下问题寻找可行的解决方案:
I am looking for a working solution for the following problem:
我在同一个网站上有多个域.
I have many domains with same website.
当有人访问 www.domainname.tld 时,应该重定向到 http://www.domainname.tld一个>
When somebody goes to www.domainname.tld there should be redirection to http://www.domainname.tld
当有人访问 subdomain.domainname.tld 时,应该重定向到 http://www.domainname.tld一个>
When somebody goes to subdomain.domainname.tld there should be redirection to http://www.domainname.tld
当有人访问 domainname.tld 时,应该重定向到 http://www.domainname.tld.
When somebody goes to domainname.tld there should be redirection to http://www.domainname.tld.
============================
===========================
我尝试了以下方法:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{SERVER_NAME}/$1 [L,R=301]
============================
===========================
问题:输入 subdomain.domainname.tld 时会重定向到 www.subdomain.domainname.tld
Problem: When entering subdomain.domainname.tld there is redirection to www.subdomain.domainname.tld
============================
===========================
我希望它在 .htaccess 中.
I want it to be in .htaccess please.
我已经搜索过了,可惜没有成功.
I have searched already, but unfortunately without success.
如果有人能提供解决方案,我会很高兴:)
I would be very happy if anybody could provide a solution :)
============================
===========================
在您的帮助下,我能够将代码更改为:
With your help I was able to change the code to:
RewriteCond %{HTTP_HOST} ^([^/.]+)\.(.*)\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%2\.%3/$1 [L,R=301]
我只需要最后一次更改 -> 它不应该指向 http://domain.tld ->我更喜欢 http://www.domain.tld
I just would need a last change -> it should not direct to http://domain.tld -> I would prefer http://www.domain.tld
我尝试了不同的尝试在目标中添加 www - 但没有任何成功.
I have tried different attempts to add the www in the target - but without any success.
我以 Anonymous 解决方案结束,这对我来说效果很好:
I ended with the Anonymous solution, which works well for me:
RewriteCond %{HTTP_HOST} !^www\.[^.]+\.[^.]+$
RewriteCond %{HTTP_HOST} ([^.]+\.[^.]+)$
RewriteRule (.*) http://www.%1/$1 [L,R=301]
感谢所有有时间阅读和帮助的人!
Thanks to all who had time to read and help!
推荐答案
没有办法删除所有的子域,但是可以得到域名的最后两部分:
There’s no way to remove all subdomains, but you can get the last two parts of the domain name:
RewriteCond %{HTTP_HOST} !^www\.[^.]+\.[^.]+$
RewriteCond %{HTTP_HOST} ([^.]+\.[^.]+)$
RewriteRule (.*) http://www.%1/$1 [L,R=301]
这篇关于如何将 .htaccess 中的子域重定向到 www.带泛型参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!