how to make mod_rewrite to redirect from subdomain

2020-04-16 18:59发布

I want to make mod rewrite as the following example :

from google.com.mydomain.com to mydomain.com/index.php?domain=google.com

I use like

RewriteCond %{HTTP_HOST} !^www [NC]
RewriteCond %{QUERY_STRING} !domain=
RewriteCond %{HTTP_HOST} ^([^\.]+)\.mydomain.com$ [NC]
RewriteRule ^(.*)$ /index.php?domain=%1 [L,QSA]

but this redirect google.mydomain.com to mydomain.com/index.php?domain=google

I want rule to redirect google.com not google

thanks

1条回答
劳资没心,怎么记你
2楼-- · 2020-04-16 19:34

It is due to the incorrect regex. Try this rule:

RewriteCond %{HTTP_HOST} !^www [NC]
RewriteCond %{QUERY_STRING} !domain=
RewriteCond %{HTTP_HOST} ^(.+?)\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ /index.php?domain=%1 [L,QSA]

In your rule you're using [^\.]+ which matched until a dot is found therefore it is matching google instead of google.com.

查看更多
登录 后发表回答