Wildcard Subdomain Rewrite

ISAPI_Rewrite is Apache mod_rewrite compatible URL rewriter for Microsoft IIS
User avatar
Posts: 4
Joined: 17 Oct 2012, 15:48

Wildcard Subdomain Rewrite

17 Oct 2012, 15:57

The code I have will append www to all URLs without www except when using localhost or the IP address. The issue I am having and cant wrap my head around is how to handle the rewrite when someone mistypes the subdomain with something like ww.domain.com it gets rewritten as http://www.ww.domain.com.

How can I fix it so that the URL gets rewritten to http://www.domain.com?

I am currently using the following rule for my site:

Code: Select all
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!(?:www\.|\d|localhost))(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]

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

Re: Wildcard Subdomain Rewrite

17 Oct 2012, 17:25

Hello,

This is an interesting issue. try using the following:

Code: Select all
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!(?:www\.|\d|localhost))(?:ww\.)?(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]


Regards
Andrew

User avatar
Posts: 4
Joined: 17 Oct 2012, 15:48

Re: Wildcard Subdomain Rewrite

17 Oct 2012, 17:41

That works for ww.domain.com but I was trying to figure out how to create a catch-all condition. So something like *.domain.com gets rewritten to www.domain.com unless the subdomain exists.

Does that make sense?

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

Re: Wildcard Subdomain Rewrite

17 Oct 2012, 17:54

you can use

Code: Select all
RewriteCond %{HTTP:Host} ^.+\domain\.com$ [NC]


but would would be your suggestion to verify the existing of the domain?

Regards
Andrew

User avatar
Posts: 4
Joined: 17 Oct 2012, 15:48

Re: Wildcard Subdomain Rewrite

18 Oct 2012, 12:30

Andrew, thanks so much for all your help. I think I am getting closer :) I am not sure how to implement the following: all URLs that meet the condition *.domain.com or is localhost or is an IP address needs to be rewritten as http://www.domain.com

This is what I came up with based off of your suggestions:

Code: Select all
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!(?:\d|localhost))(?:.+\domain\.com)?(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]

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

Re: Wildcard Subdomain Rewrite

18 Oct 2012, 13:01

Sometimes it's better to use several rules not to mess up the good thing.
Make a separate rule for IP address not to mess up the existing, it's already getting cumbersome.

Regards
Andrew

User avatar
Posts: 4
Joined: 17 Oct 2012, 15:48

Re: Wildcard Subdomain Rewrite

18 Oct 2012, 13:38

Again, I really appreciate your help with this. I feel like my mind is going to explode lol. I tried to to create a new rule but when I applied it, the webpage had a redirect loop in it. Can you help this extreme novice figure out what I am doing wrong? Here is my complete htaccess file: I am using domain as a replacement for my actual domain just for posting purposes)

Code: Select all
# Helicon ISAPI_Rewrite configuration file
# Version 3.1.0.87

RewriteEngine on

#---rewrite all subdomains to include www ---
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^.+\domain\.com$ [NC]
RewriteRule ^(.*)$ http(?%1s)://www.domain.com/$1 [R=301,L]

#---append www to all URLs without it ---
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!(?:www\.|\d|localhost))(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]

#---don't do anything for images/css/js leave protocol as is ---
RewriteRule \.(gif|jpeg|png|css|js)$ - [NC,L]

#---force https for /giftcards/default.asp ---
RewriteCond %{HTTPS} !on
RewriteRule ^giftcards/(default|giftcards)\.asp$ https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]

#---redirect all https traffic to http, unless it is pointed at /giftcards/default.asp ---
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(giftcards/default\.asp|giftcards/giftcards\.asp|social-media)(.*)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#---Rewrite Mapfile ---
RewriteMap mapfile txt:mapfile.txt [NC]
RewriteCond ${mapfile:$1|NOT_FOUND} !NOT_FOUND
RewriteRule (.*) ${mapfile:$1} [NC,R=301]

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

Re: Wildcard Subdomain Rewrite

18 Oct 2012, 20:15

First, I'd replace
Code: Select all
RewriteCond %{HTTP:Host} ^.+\domain\.com$ [NC]

with
Code: Select all
RewriteCond %{HTTP:Host} ^.+\.domain\.com$ [NC]


The best thing is to insert your rule with IP, enable logging, run testing request, disable logging and take a look at what rewrite.log has caught.
Logging issues described in FAQ

Regards
Andrew

Return to ISAPI_Rewrite 3.0

Who is online

Users browsing this forum: No registered users and 10 guests