Rewrite rule

Topics: Developer Forum
Aug 10, 2009 at 5:05 AM

Hi all.

I have to rewrite rules for website have urls like.

http://www.domain.com

http://subdomain.domain.com (there are more than one subdomain)

In the IIS the main domain and the subdomains are maped to same IP address. i.e http://www.domain.com and http://subdomain.domain.com is the same thing. i.e domain and subdomain will browse the index page on root folder.

Now using the URL rewriting i want to map the main domain to root folder while the subdomains to different folders.

e.g http://www.domain.com browse the index page on the root folder.

while http://subdomain1.domain.com browse the index page on the root/subdomain1 folder and

while http://subdomain2.domain.com browse the index page on the root/subdomain2 folder

while http://subdomain3.domain.com browse the index page on the root/subdomain3 folder

while http://subdomain4.domain.com browse the index page on the root/subdomain4 folder

and so on.

Now I want to rewrite a cond directive and rule directive that check if the url is http://www.domain.com then don't rewrite the url otherwise rewrite the url some like this

rewrite condition (if there is subdomain.domain.com) then

rewrite rule   map to                         http://www.domain.com/subdomain/

Thanks to All.

 

 

Aug 11, 2009 at 4:11 AM

Hi no one here to understand my question and help. I m waiting for very quick responce.

Coordinator
Aug 11, 2009 at 5:04 PM
Edited Aug 11, 2009 at 5:05 PM

Yes, you can do this with something like the following:

# IsapiRewrite4.ini
#
# ini file for routing by hostname
#

# IterationLimit
#
# Specifies the limit of mapping iterations for a single URL request. The default is 8. 
IterationLimit 10

# MaxMatchCount
#
# Specifies the maximum number of sub-expression matches to capture for a single pattern. 
# The default is 10. 
MaxMatchCount 10

RewriteEngine ON
StatusUrl /iirfStatus
RewriteLog c:\logs\iirf
RewriteLogLevel 3

# use * instead of % for back-references
CondSubstringBackrefFlag *

# With a request like http://host1.domain.com/Alpha.htm, if 
# the directory c:\docroot\host1 exists, then rewrite the request
# to /host1/Alpha.htm.  
# This will work if  c:\docroot is the path for the master virtual
# directory, and within that directory, there is a different
# Subdirectory for each host name.

RewriteCond  %{HTTP_HOST}   ^([^\.]+)\.domain\.com$
RewriteCond  c:\docroot\*1  -d
RewriteRule  ^/(.*)$        /*1/$1                   [L]

# May want to perform some additional processing here, for requests 
# that arrive here.  It means either the request did not match, or 
# no appropriate subdirectory exists. 

This is also now covered in the Online doc for IIRF. Look for the "Route by Hostname" example.

 

Aug 12, 2009 at 10:03 AM

Thanks. This works great. However I think if we modify the first condition like below it will be perfect so that  www.domain.com  would not rewrite to www.domain.com/www

RewriteCond %{HTTP_HOST} ^(?!www)([^/]+)\.domain\.com$
RewriteCond  c:\websiterootpath\*1  -d
RewriteRule  ^/(.*)$   /*1/$1                          [L]

 

Also can you little explain *1 and [L]

Thanks once again.

Coordinator
Aug 12, 2009 at 1:30 PM

Yes, I figured you would want to do something different with the main server name.

*1 is a back-reference.  It refers to the 1st captured group from the previous RewriteCond. 

[L] means "last rule" (don't process any more rules for this URL)

 

This is all very nicely explained in the doc, which I encourage you to view.  If you're going to deploy the thing, you need to understand how it works.

Aug 18, 2009 at 4:19 AM

Thanks for your help. I have successfully deploy the thing which is now working perfect.

Thanks once again.