简体   繁体   English

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

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

I have tried solving this on my own but I can't seem to figure it out.我试过自己解决这个问题,但我似乎无法弄清楚。

Here is what I'm trying to do:这是我正在尝试做的事情:

Create a 301 redirect of all URLs (including index) on subdomain.example.com to example.com .subdomain.example.com上的所有 URL(包括索引)创建 301 重定向到example.com

So for example:例如:

  • subdomain.example.com redirects to example.com subdomain.example.com重定向到example.com
  • subdomain.example.com/blog-post redirects to example.com subdomain.example.com/blog-post重定向到example.com

This means that I DO NOT want to keep the same URL structure that's used on subdomain.example.com.这意味着我不想保留在 subdomain.example.com 上使用的相同 URL 结构。 I just want URLs on the subdomain to point to the index on the example.com.我只希望子域上的 URL 指向 example.com 上的索引。 I know that's not the best solution for SEO, but that's how I want it.我知道这不是 SEO 的最佳解决方案,但这就是我想要的。

I have tried accomplish this using我尝试过使用

Redirect 301 / https://example.com/

but that keeps the URL structure and I don't want that.但这保留了 URL 结构,我不希望这样。

So how can I solve this?那么我该如何解决呢?

You can not achieve this with a Redirect as it appends the old URL path to new one.您无法通过Redirect实现此目的,因为它将旧的 URL 路径附加到新路径。 You can use RewriteRule您可以使用RewriteRule

RewriteEngine on

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

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

keeps the part of the url.保留 url 的一部分。

With redirectMatch you can use regexes and substitution to rewrite your url eg使用redirectMatch,您可以使用正则表达式和替换来重写您的 url 例如

RedirectMatch 301 . https://example.com/

where the dot (.) is a match anything.其中点 (.) 是匹配任何东西。

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

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