将子域重定向到域,同时保留路径和查询字符串



我有一个子域,我想重定向到主域(使用 .htaccess),例如:

  1. https://abc.example.com我想将其重定向到https://www.example.com
  2. https://abc.example.com/path/page-name to https://www.example.com/path/page-name
  3. https://abc.example.com/path/page-name?test=12&test1=12https://www.example.com/path/page-name?test=12&test1=12

请建议我如何做。

我已经尝试了以下解决方案,但它不起作用。

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+).example.com$   [NC]
RewriteRule ^ http://www.example.com/suberror  [L,R]

我正在使用Laravel。

在此行:

RewriteRule ^ http://www.example.com/suberror  [L,R]

没有 pattern的含义,对请求的URI检查了以至于REGEX。

将其更改为:

RewriteRule ^(.*)$ http://www.example.com/$1  [L,R]

此部分^(.*)$pattern,将通过此$1

substitution中呈现

如果可以,则将[L,R]更改为[L,R=301]为永久重定向,因为仅R即表示临时的R=302

假设:

  • 您有1个子域(如示例中所述)
  • 子域和主要域指向文件系统上的同一区域(它们共享一个共同的根)。

在您的.htaccess文件顶部尝试以下内容:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^abc.example.com [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI}  [R,L]

请求的URL路径保存在REQUEST_URI服务器变量中。该请求的查询字符串将传递到替换(target),而无需任何其他工作。

最新更新