【问题标题】:htaccess redirect with full urlhtaccess 使用完整的 url 重定向
【发布时间】:2016-03-31 08:52:59
【问题描述】:

是否可以使用 htaccess 从一个域重定向到另一个域?

RewriteRule http://www.google.com/old-route http://www.google.com/new-route [R=301,L]
RewriteRule https://www.google.com/old-route https://www.google.com/new-route [R=301,L]

如果没有,您将如何在一个项目中对多个域进行重定向?

【问题讨论】:

  • 您在源 URL 和目标 URL 中显示相同的 www.google.com

标签: php .htaccess redirect


【解决方案1】:

这可以通过使用 rewriteConditions 来实现:https://wiki.apache.org/httpd/RewriteCond

类似的东西:

RewriteCond %{HTTP_HOST} ^site1
RewriteRule ^old-route$ https://www.site1alt.com/new-route [R=301,L]

【讨论】:

    【解决方案2】:

    鉴于https://www.example.com/foobar 其他答案中使用的单个%{HTTP_HOST} 仅查找www.example.com,而不考虑以下路径。所以这些答案对我不起作用。也许在 Apache 版本期间发生了一些变化。对我有用的是连接 %{HTTP_HOST}%{REQUEST_URI} 以进行检查。

    RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www\.example\.com/foo\.html$ [NC,OR]
    RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www\.example\.com/bar\.html$ [NC]
    RewriteRule ^(.*)$ https://www.example.com/foobar [R=301,L]
    

    查看这个漂亮的Mod_Rewrite Variables Cheatsheet,了解许多可用的 mod_rewrite 变量的概述。

    【讨论】:

      【解决方案3】:

      供一个域使用。

      RewriteEngine On
      RewriteBase /
      
      RewriteCond %{HTTP_HOST} ^www\.google\.com/old-route$ [NC]
      RewriteRule ^(.*)$ http://www.google.com/new-route [R=301,L]    
      

      为了重定向所有你可以使用的东西。

      Redirect 301 / http://www.google.com/new-route
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-08
        • 2016-08-21
        • 2013-12-05
        相关资源
        最近更新 更多