【问题标题】:redirect one domain to the other while removing a subdirectory from the uri将一个域重定向到另一个域,同时从 uri 中删除子目录
【发布时间】:2015-08-20 14:44:17
【问题描述】:

我正在尝试重定向第三级域 (sub.domain1.com),但前提是它与子目录匹配:

http://sub.domain.com/somefolder

这将被重定向到另一个域(domain2.com),但需要删除 /somefolder 部分,所以这样说:

http://sub.domain.com/somefolder/about

重定向时会这样读:

http://domain.com/about

这是我迄今为止尝试过的,但似乎不起作用:

RewriteEngine On
RewriteCond %{HTTP_HOST} (^|\.)sub\.domain1\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/somefolder$ [NC]
RewriteRule ^/somefolder/$ https://domain2.com/$ [R=301,L]

谁能告诉我我做错了什么?我开始理解 htaccess,但它对我来说仍然像巫术一样......

注意:两个域都指向同一个服务器,除了 domain1.com 指向根 Web 文件夹和 domain2.com 指向 /webroot/somefolder,因此它是根内部的子目录。

【问题讨论】:

    标签: .htaccess mod-rewrite http-status-code-301


    【解决方案1】:

    您需要在正则表达式中添加分组,并在重定向中添加反向引用:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} (^|\.)sub\.domain1\.com$ [NC]
    RewriteRule ^somefolder/(.*)$ https://domain2.com/$1 [R=301,L]
    

    而且您不需要条件来检查请求 URI,因为您已经在规则的正则表达式中这样做了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 2014-06-26
      • 1970-01-01
      • 2019-01-12
      • 2013-08-04
      • 1970-01-01
      相关资源
      最近更新 更多