Domain formatting problems

ISAPI_Rewrite is Apache mod_rewrite compatible URL rewriter for Microsoft IIS
User avatar
Posts: 14
Joined: 18 Sep 2013, 10:49

Domain formatting problems

18 Sep 2013, 11:01

Hi, wonder if you could help me. I've been using ISAPI Rewrite for years now successfully on a simple www domain, but i've added a mobile subdomain and have run into issues.

So, I have www dot mydomain.com and m dot mydomain.com

I always want to prefix anything.mydomain.com (with the exception of m dot mydomain.com) with www dot mydomain.com to make my URLs standard and not affect SEO. So, I had this, which works:

#prefix always with www.
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]


Having launched my m.mydomain.com subdomain, I changed the above to this:
#prefix always with www.
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{HTTP:Host} ^(?!m\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]


I also have a number of other rules that make sure that if anyone visits my site on a different TLD then I rewrite it to the .com. For example:
RewriteCond %{HTTP_HOST} ^(?:www\.)?mydomain\.org$ [NC]
RewriteRule (.*) http://www.mydomain.com/$1 [NC,L,R=301]


However, since adding the m.mydomain.com subdomain I'm getting issues. For example, if I entered an incorrect subdomain like a.mydomain.com it resolves to this url:
www.a.mydomain.coma.mydomain.com/
There seems to be some sort of repetition going on and prefixing with www. instead of replacing the a. to www.

I'm also getting visitors hit my homepage www.mydomain.com and getting this in the url bar of their browser:
www.www.www.www.www.www.www.www.www.www ... omain.com/
Seems to be some sort of loop I've not closed off.

So, my requirement in summary is to allow visitors on two addresses:
http://www.mydomain.com
m.mydomain.com
and no others. And, to rewrite any non-compliant domains to www.mydomain.com

Any help would be much appreciated.

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: Domain formatting problems

20 Sep 2013, 00:10

Hello,

My question to you would be: If you're using it only on one domain, why don't you simply hardcode your domain name here, as following:

Code: Select all
#prefix always with www.
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(myDomain\.com)$ [NC]
RewriteCond %{HTTP:Host} ^(?!m\.)(myDomain\.com)$ [NC]
RewriteCond %{HTTP:Host} ^(?:.*\.)?(myDomain\.com)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]


We can even try to make it more compact:

Code: Select all
#prefix always with www.
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!(?:www|m)\.)(myDomain\.com)$ [NC]
RewriteCond %{HTTP:Host} ^(?:.*\.)?(myDomain\.com)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%4 [R=301,L]


Note that in the last variant I use %4, since we've done some modifications, it seems that REQUEST_URI will be %4, not %3

Regards
Andrew

Return to ISAPI_Rewrite 3.0

Who is online

Users browsing this forum: No registered users and 21 guests