整个域的 301 重定向 (.htaccess) 到另一个域索引

301 redirect (.htaccess) of entire domain to another domain index

我已经尝试自己解决这个问题,但我似乎无法弄清楚。

这是我正在尝试做的事情:

创建 subdomain.example.com 上所有 URL 的 301 重定向(包括索引)到 example.com.

例如:

这意味着我不想保留 subdomain.example.com 上使用的相同 URL 结构。我只希望子域上的 URLs 指向 example.com 上的索引。我知道这不是 SEO 的最佳解决方案,但这就是我想要的。

我尝试使用

完成这个
Redirect 301 / https://example.com/

但这保留了 URL 结构,我不想要那个。

那我该如何解决呢?

您无法使用 Redirect 实现此目的,因为它将旧 URL 路径附加到新路径。您可以使用 RewriteRule

RewriteEngine on

RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteRule (.*) https://example.com/ [R=301,L]

根据文档https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect

保留url的部分。

借助 redirectMatch,您可以使用正则表达式和替换来重写您的 url 例如

RedirectMatch 301 . https://example.com/

点 (.) 可以匹配任何内容。