简体   繁体   English

将所有子域重定向到它们各自的普通域 in.htaccess

[英]Redirect all subdomains to their respective normal domain in htaccess

I am using WordPress Multiste on CyberPanel.我在 CyberPanel 上使用 WordPress Multiste。 The child sites have their own mapped domains.子站点有自己的映射域。 I need all wildcard subdomains redirecting to their respective regular domain (without www)我需要将所有通配符子域重定向到它们各自的常规域(没有 www)

For example, I have following sites.例如,我有以下网站。 mainsite.com site2.com site3.com mainsite.com site2.com site3.com

Currently: anything.site2.com redirects to mainsite.com.当前:anything.site2.com 重定向到 mainsite.com。 Similarly, anything.site3.com redirects to mainsite.com同样,anything.site3.com 重定向到 mainsite.com

What I want is that: anything.site2.com redirects to site2.com, anything.site3.com redirects to site3.com, and so on.我想要的是:anything.site2.com 重定向到 site2.com,anything.site3.com 重定向到 site3.com,等等。

What code should I add to .htaccess so that wildcard subdomains are redirected as mentioned?我应该向 .htaccess 添加什么代码,以便如前所述重定向通配符子域?

Well, you want to match any requested host name inside any ".com domain", extract the base domain name from that and redirect any request to it:好吧,您想匹配任何“.com 域”中的任何请求的主机名,从中提取基本域名并将任何请求重定向到它:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:.+)\.([^.]+\.com)$
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

Update for the question you ask in the first comment to this answer:更新您在此答案的第一条评论中提出的问题:

Sure, you could repeat the rule for all TLDs that are relevant, but that is only necessary if you want to use different rules.当然,您可以为所有相关的 TLD 重复该规则,但只有在您想要使用不同的规则时才需要这样做。 If you are not interested in the TLD at all, then just use a wildcard:如果您对 TLD 根本不感兴趣,那么只需使用通配符:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:.+)\.([^.]+\.[^.]+)$
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

If you want to accept just certain TLDs, then do that:如果您只想接受某些 TLD,请执行以下操作:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:.+)\.([^.]+\.(?:com)|(?:org)|(?:info))$
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

Or you can apply the rule just to specific domains:或者您可以将规则仅应用于特定域:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:.+)\.(exampleA.com)$ [OR]
RewriteCond %{HTTP_HOST} ^(?:.+)\.(exampleB.org)$ [OR]
RewriteCond %{HTTP_HOST} ^(?:.+)\.(exampleC.info)$
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

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

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