将一个域重定向到另一个域,同时从 uri 中删除子目录
redirect one domain to the other while removing a subdirectory from the uri
我正在尝试重定向第三级域 (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 之外,两个域都指向同一台服务器,因此它是根目录中的一个子目录。
您需要在正则表达式中添加分组并在重定向中添加反向引用:
RewriteEngine On
RewriteCond %{HTTP_HOST} (^|\.)sub\.domain1\.com$ [NC]
RewriteRule ^somefolder/(.*)$ https://domain2.com/ [R=301,L]
而且您不需要条件来检查请求 URI,因为您已经在规则的正则表达式中这样做了。
我正在尝试重定向第三级域 (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 之外,两个域都指向同一台服务器,因此它是根目录中的一个子目录。
您需要在正则表达式中添加分组并在重定向中添加反向引用:
RewriteEngine On
RewriteCond %{HTTP_HOST} (^|\.)sub\.domain1\.com$ [NC]
RewriteRule ^somefolder/(.*)$ https://domain2.com/ [R=301,L]
而且您不需要条件来检查请求 URI,因为您已经在规则的正则表达式中这样做了。