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

问题描述 投票:0回答:2

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

  1. https://abc.example.com我想将它重定向到https://www.example.com
  2. https://abc.example.com/path/page-namehttps://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。

laravel .htaccess mod-rewrite url-redirection
2个回答
1
投票

在这一行:

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

没有pattern的意思,正则表达式检查请求的URI。

把它改成这个:

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

这部分^(.*)$pattern,它将在substitution$1呈现

如果它是好的,将[L,R]更改为[L,R=301]是永久重定向,因为R单独意味着R=302是临时的。


1
投票

假设:

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

.htaccess文件的顶部尝试类似下面的内容:

RewriteEngine On

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

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

© www.soinside.com 2019 - 2024. All rights reserved.