【问题标题】:How do I make this work using .htaccess - redirect main domain to subdomain & redirect all other pages to new domain如何使用 .htaccess 进行这项工作 - 将主域重定向到子域并将所有其他页面重定向到新域
【发布时间】:2018-11-07 03:38:22
【问题描述】:

我有example.com,我可以将其重定向到home.example.com

使用:

RewriteEngine On
RewriteRule (.*) https://home.example.com/$1 [R=301,L]

但我想做的另一部分是将所有其他 URL 重定向到新域。

https://example.com/interestingblogpost 重定向到https://another.example/interestingblogpost

任何建议,我似乎在搜索中找不到任何东西。

哦,这是一个 WordPress 网站。

【问题讨论】:

    标签: wordpress .htaccess mod-rewrite


    【解决方案1】:

    您当前的指令已将所有 URL 重定向到另一个主机名(主域的子域)。

    如果您需要进行特定的重定向,那么您只需将这些放在您当前的指令之前。 IE。最具体的重定向应该放在第一位。

    例如:

    RewriteEngine On
    
    # Specific redirects...
    RewriteRule ^interestingblogpost$ https://another.example/$0 [R=301,L]
    
    # Redirect everything else to home.example.com
    RewriteRule (.*) https://home.example.com/$1 [R=301,L]
    

    假设这个.htaccess 文件只服务于example.com,那么上面的代码会将https://example.com/interestingblogpost 重定向到https://another.example./interestingblogpost

    $0 是整个匹配模式的反向引用。

    您需要在测试之前清除浏览器缓存,因为之前重定向所有内容的 301 可能已被缓存(如果您之前请求过 /interestingblogpost)。出于这个原因,通常最好先使用默认不缓存的 302(临时)重定向进行测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-23
      • 2015-02-13
      • 1970-01-01
      • 2019-12-20
      • 1970-01-01
      • 2010-12-29
      • 2011-09-24
      • 1970-01-01
      相关资源
      最近更新 更多