使用 htaccess 重定向所有子域

redirect all subdomains with htaccess

我有一些未使用的子域,我想将它们重定向到我的主页。

我找到了这个 .htaccess 代码

# redirect all subdomains
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com/ [L,R]

当您调用确切的子域时它会重定向,例如:test.example.com

但是如果你调用其他任何东西它就不起作用,例如:test.example.com/meh

是否可以将这些子站点中的任何 URL 重定向到主页?

您可以使用这条规则:

# redirect all subdomains
RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301,NE]

%{REQUEST_URI}在目标末尾会将之前的 URI 添加到新目标。