我有一个子域名,我想重定向到主域名(使用.htaccess
),如下所示:
https://abc.example.com
我想将它重定向到https://www.example.com
https://abc.example.com/path/page-name
到https://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。
把它改成这个:
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R]
这部分^(.*)$
是pattern
,它将在substitution
由$1
呈现
如果它是好的,将[L,R]
更改为[L,R=301]
是永久重定向,因为R
单独意味着R=302
是临时的。
假设:
在.htaccess
文件的顶部尝试类似下面的内容:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^abc\.example\.com [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R,L]
来自请求的URL路径保存在REQUEST_URI
服务器变量中。来自请求的查询字符串将传递给替换(目标),而无需任何其他工作。