【问题标题】:Redirecting Urls from old website to New将网址从旧网站重定向到新网站
【发布时间】:2017-09-25 16:31:16
【问题描述】:

我正在尝试使用 htaccess 将我的网站从旧域重定向到新域。两个网站都具有相同的页面,具有相同的 url 结构我只需要将主要 url 部分即 https://test.com 旧 url 重定向到 https://test1.com 以及其他页面 url。例如,如果我在旧网站 (https://test.com/page1) 中有一个页面,它应该被重定向到 (https://test1.com/page1)。我使用此代码重定向它,但它没有任何帮助。

Redirect 301 /page1/ https://test1.com/page1

Redirect 301 https://test1.com

【问题讨论】:

标签: .htaccess redirect


【解决方案1】:

您可能会发现这些有用:

#Redirect from old domain to new domain
RewriteEngine on
RewriteBase /
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

#Redirect from old domain to new domain with full path and query string:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*) http://www.newdomain.com%{REQUEST_URI} [R=302,NC]

欢迎关注资源:https://gist.github.com/ScottPhillips/1721489

【讨论】:

    【解决方案2】:

    第一个 Redirect 具有不同的源和目标 URL 路径。旧 URL 有一个尾部斜杠,而新 URL 没有。

    那么任何以 URL-path 开头的请求都会在目标 URL 的位置向客户端返回一个重定向请求。匹配的 URL 路径之外的其他路径信息将附加到目标 URL。

    这意味着,如果您要求

    http://old.example.com/page1/some/script.php
    

    /page1/ 之外的路径(一些/script.php 没有 前导斜杠)将被采用并附加到目标/page1

    http://new.example.com/page1some/script.php
    

    要附加完整路径,您必须省略旧 URL 路径中的尾部斜杠,例如

    Redirect /page1 https://test1.com/page1
    

    要解决第二个Redirect,这仅适用于Location 指令,例如

    <Location "/page1">
        Redirect https://test1.com/page1
    </Location>
    

    最后,当一切正常时,您可以将状态码更改为301从不301测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      • 1970-01-01
      • 2016-11-28
      • 2013-06-05
      • 2010-11-17
      • 2021-10-21
      • 2021-06-22
      相关资源
      最近更新 更多