简体   繁体   English

.htaccess 301将旧域名重定向到新域,从托管根目录到子文件夹,可以运行,但是为什么呢?

[英].htaccess 301 redirect old to new domain, from hosting root to subfolder, working but why?

The scenario: I've moved a WordPress website to a new domain and want to 301 redirect all the pages from the old domain to the new domain. 场景:我已将WordPress网站移至新域,并希望301将所有页面从旧域重定向到新域。 Both sites are on the same hosting account running Apache. 两个站点都在运行Apache的同一主机帐户上。 The old site is at the root level (public_html), and the new site is in a subfolder (below/inside the root). 旧站点位于根目录(public_html),新站点位于子文件夹(位于根目录的下方/内部)。

I've managed to make this work, but I'd like to learn and understand why it works. 我设法完成了这项工作,但我想学习并了解它的工作原理。 So below is a quick overview of my 'journey' and solution, together with three specific questions. 因此,以下是我的“旅程”和解决方案的快速概述,以及三个具体问题。

First I tried to do the redirects like this (code added to the root .htaccess file): 首先,我尝试进行这样的重定向(将代码添加到根.htaccess文件中):

# 301 Page Redirects - not working - causes redirect loop

redirect 301 /  https://new-domain.com/     
redirect 301 /services/ https://new-domain.com/services/    
redirect 301 /recipes/  https://new-domain.com/recipes/ 

But this causes a redirect loop. 但这会导致重定向循环。 I'm guessing because the .htaccess file with these rules is at the root level and therefore also affects the subfolders. 我猜是因为带有这些规则的.htaccess文件位于根级别,因此也影响子文件夹。

Question 1: Is my assumption above about the reason for the redirect loop correct? 问题1:以上关于重定向循环原因的假设是否正确?

Then I tried to be more specific and put this code in the root .htaccess file instead: 然后,我尝试更加具体,然后将以下代码放在根.htaccess文件中:

# 301 Page Redirects - not working - does nothing at all - not sure why 

redirect 301 https://old-domain.com/ https://new-domain.com/    
redirect 301 https://old-domain.com/services/ https://new-domain.com/services/  
redirect 301 https://old-domain.com/recipes/ https://new-domain.com/recipes/

I was hoping the above code would do the trick, because it's more specific about the old domain. 我希望上面的代码可以解决问题,因为它对旧域更具体。 My thinking was that it specifies the old domain exactly and so would circumvent the redirect loop. 我的想法是,它准确地指定了旧域,因此可以绕过重定向循环。 But instead this code seems to have no effect at all. 但是相反,此代码似乎根本没有任何作用。 The redirect loop was gone, but now no redirects were happening anymore at all. 重定向循环消失了,但是现在根本没有任何重定向发生。

Question 2: Why would the above code not produce any redirects at all? 问题2:为什么上述代码根本不会产生任何重定向?

Then I found this answer and applied the code from that, which works perfectly and creates all the redirects. 然后,我找到了这个答案,并从中应用了代码,该代码可以完美地工作并创建所有重定向。 Plus it's much more elegant than my previous attempts above. 此外,它比上面我之前的尝试要优雅得多。 This is the code: 这是代码:

# 301 Redirects from old-domain.com to new-domain.com - THIS CODE WORKS - Yay!

RewriteEngine On
RewriteCond %{HTTP_HOST} ^old-domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old-domain.com$
RewriteRule (.*)$ https://new-domain.com/$1 [R=301,L]

Question 3: Why does this code not cause any redirect loops when I place it in the root .htaccess? 问题3:当我将其放在根.htaccess根目录中时,为什么此代码不会引起任何重定向循环?

I realise I'm copy/pasting code without fully understanding why it works. 我意识到我在复制/粘贴代码时并没有完全理解它的工作原理。 So I'd love an explanation in simple terms about these behaviours. 因此,我希望以简单的方式对这些行为进行解释。 Thank you. 谢谢。

Answer 1: 答1:

From the info that you have provided, I would say no. 根据您提供的信息,我会拒绝。 You did not specify if the new-domain.com website is configured (in apache configuration) with its document root being public_root or public_root/subfolder (judging by the described behaviour I would say it is the former). 您未指定new-domain.com网站是否已配置(以apache配置),其文档根目录为public_root或public_root / subfolder(根据描述的行为判断,我会说是前者)。 In that case, when you request https://old-domain.com/anything , the server will (because of the unconditional redirect in your first rule) respond with redirect to https://new-domain.com/anything . 在这种情况下,当您请求https://old-domain.com/anything ,服务器将(由于第一个规则中的无条件重定向)响应重定向到https://new-domain.com/anything Client browser will then request that URL and it will hit the same Apache and same .htaccess, which will again result in the same redirect, causing the loop. 然后,客户端浏览器将请求该URL,它将命中相同的Apache和相同的.htaccess,这将再次导致相同的重定向,从而导致循环。

Answer 2: 答案2:

Redirect syntax : 重定向语法

 Redirect [status] [URL-path] URL 

The old URL-path is a case-sensitive (%-decoded) path beginning with a slash. 旧的URL路径是区分大小写(%解码)的路径,以斜杠开头。

In your rule, you are specifying [URL-path] as https://old-domain.com/ , which is wrong: it can be / , /services/ , or /recipes , but not https://old-domain.com/ or https://old-domain.com/services/ . 在您的规则中,您将[URL-path]指定为https://old-domain.com/ ,这是错误的:它可以是//services//recipes ,但不能是https://old-domain.com/https://old-domain.com/services/ The request [URL-path] does not match [URL-path] specified in your rule, so redirect never happens. 请求[URL-path]不匹配[URL-path]在您的规则中指定,所以重定向从未发生过。

Answer 3: 答案三:

This basically does the same thing as your first rule in Answer 1., with one important difference: the server will respond with redirect only if the hostname in request (or to be more precise, the content of the Host: header in request) is equal to old-domain.com or www.old-domain.com , which will prevent the loop since the second request from the client will use new-domain.com hostname. 这基本上与答案1中的第一个规则具有相同的作用,但有一个重要的区别:仅当请求中的主机名(或更确切地说,请求中的Host:标头的内容)为等于old-domain.comwww.old-domain.com ,这将阻止循环,因为来自客户端的第二个请求将使用new-domain.com主机名。

Also, from the above, seems like your "new" website in subfolder will never be served: either if old-domain.com or new-domain.com is requested, the site from public_html folder will be shown (and only the hostname in clients browser address bar will change). 另外,从上面看来,您子目录中的“新”网站似乎永远也不会提供服务:如果请求old-domain.comnew-domain.com ,则将显示public_html文件夹中的网站(仅显示主机名客户端浏览器的地址栏将会更改)。

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

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