redirect subdomain to same subdomain name but different domain name

馋奶兔 提交于 2019-12-22 12:54:11

问题


i have site called exmaple.com have a lot of subdomains

test1 , test2 , test2.example.com

how could i redirect subdomains with exact subdomain but to another domain name for example

test1 , test2 , test3.example.net

thanks


回答1:


This may work. Put it in your .htaccess.

RewriteEngine On
RewriteCond %{HTTP_HOST} (.*).example.com
RewriteRule (.*) http://%1.example.net/$1 [R=301,QSA,L]

Haven't tested it, but it should give you a start.




回答2:


Use %1 %2 etc. to grab regex-match groups from the RewriteCond.

As you probably know, $1 $2 matches regex groups from the RewriteRule

Off the top of my head, this is pretty straightforward with mod_rewrite active:

 RewriteEngine On
 RewriteCond %{REQUEST_HOST} (.+)\.example\.com
 RewriteRule (.*) http://%1.example.net$1 [R,QSA]

This assumes that you are serving all those subdomains from a single virtual host, but if you aren't, you can just copy this ruleset into each one's config or .htaccess individually.



来源:https://stackoverflow.com/questions/10507548/redirect-subdomain-to-same-subdomain-name-but-different-domain-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!