Specific Subdomain Redirects

ISAPI_Rewrite is a powerful URL manipulation engine based on regular expressions
User avatar
Posts: 5
Joined: 22 Apr 2014, 09:44

Specific Subdomain Redirects

22 Apr 2014, 09:50

Hi, here is my problem...

I need to redirect mydomain from non-www to the www. BUT i need to preserve specific subdomains...

Eg.

http://mydomain.com should redirect to http://www.mydomain.com

http://subdomain1.mydomain.com should not redirect and remain the same

http://subdomain2.mydomain.com should redirect to http://www.mydomain.com because i do not want this subdomain.

Basically I have about 5 different subdomains which shouldn't get captured by the non-www redirect, but anything else needs to redirect to www.

Is this possible?

User avatar
Posts: 871
Joined: 12 Mar 2012, 09:54

Re: Specific Subdomain Redirects

23 Apr 2014, 06:37

Hello,

Here's how the rule should look like:

[ISAPI_Rewrite]
RewriteCond Host: ^(?!(?:www|subdomain1\.|something\.|else\.|you\.|need\.|to\.|exclude\.).*)(.*)?mydomain\.com
RewriteRule (.*) http://www.mydomain.com$2 [I,RP]

User avatar
Posts: 5
Joined: 22 Apr 2014, 09:44

Re: Specific Subdomain Redirects

23 Apr 2014, 21:33

Hi Anton

This is fantastic! Many thanks!

Just a thought, the rule you've posted is a permanent redirect... If at some point in the future I wanted to use subdomain1, would the permanent redirect prevent browsers/servers from accessing this subdomain in the future, even if i add it as a permitted subdomain at that point?

non-www. and subdomain1 should redirect permanently, but should subdomain2 "302" redirect instead? How would I achieve this? Would I need two separate rules that don't conflict with each other?

User avatar
Posts: 871
Joined: 12 Mar 2012, 09:54

Re: Specific Subdomain Redirects

24 Apr 2014, 05:21

"If at some point in the future I wanted to use subdomain1, would the permanent redirect prevent browsers/servers from accessing this subdomain in the future, even if i add it as a permitted subdomain at that point?"
- If you want to use subdomain1 again, just remove it from the rule I have provided and requests to it will no longer be redirected.

If you need a 302 redirect, use [R] instead of [RP] flag.

"Would I need two separate rules that don't conflict with each other?"
- Yes, I believe so.

User avatar
Posts: 5
Joined: 22 Apr 2014, 09:44

Re: Specific Subdomain Redirects

24 Apr 2014, 06:47

Hi Anton, thanks for the response!

I've probably not explained myself properly...

What I meant was that using the above rule, the invalid domain subdomain1 correctly redirects to the www. version of the domain, but with a permanent redirect. If I subsequently add this domain as a valid subdomain at some point, browers and servers that have already cached the 301 redirect will never reach the valid subdomain and always redirect to the www. version.

I suppose what i'm asking, is there a way to redirect invalid subdomains on a 302 basis, but valid subdomains pass through on a 301 redirect?

User avatar
Posts: 871
Joined: 12 Mar 2012, 09:54

Re: Specific Subdomain Redirects

28 Apr 2014, 05:33

Hello,

As far as we know it is incorrect to cache redirects and very few servers/browsers do that, so there shouldn't be any problem with making the subdomain accessible again.

User avatar
Posts: 5
Joined: 22 Apr 2014, 09:44

Re: Specific Subdomain Redirects

28 Apr 2014, 09:44

Hi Anton

Thanks for the info! I use Firefox and it does cache 301 redirects meaning I have to flush it if I make a mistake. Often my users will miss-type something or search servers will try different combinations of keywords to find results. For example they may try [email protected] and whilst i'm not currently using that subdomain, if I do at a later date, there is a chance they will never see it. Not a big problem, but still a problem.

User avatar
Posts: 871
Joined: 12 Mar 2012, 09:54

Re: Specific Subdomain Redirects

29 Apr 2014, 06:10

Hello,

You can set "no-cache" for 301 redirects, cause there is nothing we can do in ISAPI_Rewrite about it.
Also check this thread for some ideas http://stackoverflow.com/questions/6980 ... -redirects

Return to ISAPI_Rewrite 2.x

Who is online

Users browsing this forum: No registered users and 0 guests