Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Url Rewriting with IIS 6/7 - Rewriting Host Name {HTTP_HOST}

Tags:

asp.net

iis

I need to rewrite the "Host Name" {HTTP_HOST} for an incoming request. Is it possible to do this using IIS 7 Rewrite Module?
I want to rewrite http://abc.xyz.com/* to http://xyz.com/sites/abc/*. This is being done for a SharePoint site which uses the {HTTP_HOST} internally.

Are there any Url Rewriters out there which let me change the {HTTP_HOST} variable of IIS?

Kind regards,

like image 260
SharePoint Newbie Avatar asked Feb 15 '09 07:02

SharePoint Newbie


4 Answers

You can use IIS 7 URL Rewrite Module 2.0 to change the HTTP_HOST server variable. More information on how to do it is available in Setting HTTP request headers and IIS server variables.

like image 59
RuslanY Avatar answered Nov 16 '22 13:11

RuslanY


IIRF is free, and lets you rewrite headers, including HTTP_HOST.

I want to rewrite http://abc.xyz.com/* to http://xyz.com/sites/abc/*. This is being done for a SharePoint site which uses the {HTTP_HOST} internally.

You need to rewrite both the Host and the Url. This makes it a little complicated. In this ruleset, I do it in steps, and use a new header to store state between the steps:

# detect whether we're using the abc host
RewriteCond     %{HTTP_HOST}          ^abc\.xyz\.com$
RewriteHeader   Host-Needs-Rewrite:   ^$                 YaHuh

# rewrite the Host: header to the alt host name if necessary
RewriteCond     %{HTTP_HOST_NEEDS_REWRITE}   ^YaHuh$
RewriteCond     %{HTTP_HOST}                 ^(?!xyz\.com)(.+)$
RewriteHeader   Host:          .*            xyz.com 

# rewrite the Url to the appropriate place
RewriteCond     %{HTTP_HOST_NEEDS_REWRITE}   ^YaHuh$
RewriteCond     %{HTTP_HOST}                 ^xyz\.com$
RewriteRule     /(.*)$                       /sites/abc/$1     [L]

You could wildcard the abc part, too. Like this:

# detect whether we're using the abc host
RewriteCond     %{HTTP_HOST}          ^([^.]+)\.xyz\.com$
RewriteHeader   Host-Needs-Rewrite:   ^$                     %1

# rewrite the Host: header to the alt host name if necessary
RewriteCond     %{HTTP_HOST_NEEDS_REWRITE}   ^.+$
RewriteCond     %{HTTP_HOST}                 ^(?!xyz\.com)(.+)$
RewriteHeader   Host:          .*            xyz.com 

# rewrite the Url to the appropriate place
RewriteCond     %{HTTP_HOST_NEEDS_REWRITE}   ^(.+)$
RewriteCond     %{HTTP_HOST}                 ^xyz\.com$
RewriteRule     /(.*)$                       /sites/%1/$1     [L]
like image 31
Cheeso Avatar answered Nov 16 '22 12:11

Cheeso


I'm not familiar with the IIS 7 Rewrite Module, but ISAPI_Rewrite can change pretty much any HTTP header you want. There's a free version which is enough for our site and may well be enough for yours.

like image 44
EMP Avatar answered Nov 16 '22 13:11

EMP


Yes, it is possible to use the IIS 7 Rewrite Module. You can use the GUI to setup a redirect in IIS7 per this blog post.

like image 2
notandy Avatar answered Nov 16 '22 12:11

notandy