Mod_Rewrite Subdomain/Path Rewriting Issue

Question!

I have an existing rule in place to rewrite:
subdomain.domain.com

to this format:
domain.com/directory/index.aspx?id=subdomain

RewriteCond %{HTTP_HOST} !^www\. [NC]<br/>
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [NC]<br/>
RewriteRule ^$ /directory/index.aspx?id=%1 [NC,QSA,L]



I need to update this to support:
subdomain.domain.com/foo

to this format:
domain.com/directory/foo.aspx?id=subdomain

I've tried to make the following changes to my existing rule without success:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com/(.*)
RewriteRule ^$ /directory/%2.aspx?id=%1 [NC,QSA,L]

It doesn't appear that the second RewriteCond is being matched. Am I missing something easy? Should this be handled differently?
Thanks.



Answers
The HTTP_HOST variable contains the value in the HTTP "Host:" header -- that is, in your case, "subdomain.domain.com". The trailing URI (slash, etc.) is not there.

You need to match that part of the URL in your rule:

RewriteCond %{HTTP_HOST} !^www\. [NC]<br/>
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [NC]<br/>
RewriteRule ^/([^/]+)$ /directory/$1.aspx?id=%1 [NC,QSA,L]

Salut,

Jordi.

By : jsalvata


This video can help you solving your question :)
By: admin