我有一个子域,我想重定向到主域(使用 .htaccess
),例如:
-
https://abc.example.com
我想将其重定向到https://www.example.com
-
https://abc.example.com/path/page-name
tohttps://www.example.com/path/page-name
-
https://abc.example.com/path/page-name?test=12&test1=12
至https://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),而无需任何其他工作。