Redirecting main site, except subdomain

时间:2019-04-17 02:42:07

标签: apache .htaccess mod-alias

I did a number of search unsuccessfully before deciding to post this.

I have a main site, say https://mainsite.com and a subdomain, say, https://subdomain.mainsite.com .

Challenge is, I want to redirect only the main site to https://mainsite.com/mysite and not the subdomain.

I tried this:

RedirectMatch ^https://mainsite\.com https://mainsite.com/mysite

This wouldn't do anything if I type https://mainsite.com

Originally, I had this in-place and it worked fine for the main site:

RedirectMatch ^/$ /mysite
RedirectMatch /mysite/ /mysite

but when I had the subdomain, it added the /mysite to the subdomain as well, which isnt what I want.

I would like to achieve this with Redirectmatch

Thanks for any help.

1 个答案:

答案 0 :(得分:1)

You can use mod-rewrite

RewriteEngine on

#if http host not sub.domain.com
RewriteCond %{HTTP_HOST} !sub\.domain\.com
redirect / to /mysite
RewriteRule ^/?$  /mysite [L,R]

On apache 2.4 you can enclose your RedirectMatch with if condition to check the host header, something like the following should work on Apache 2.4 or more

 <If "%{HTTP_HOST} == 'example.com'">
RedirectMatch ^/$ /mysite/
</If>

This will redirect / to http://example.com/mysite if the host is example.com .

相关问题