tags:

views:

7116

answers:

6

Hi,

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,

+1  A: 

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.

Evgeny
I don't want to doa redirect. Instead I want to change the {HTTP_HOST} for the current request.
SharePoint Newbie
ISAPI_Rewrite can do exactly that - it can change the "Host:" HTTP header for the current request, just as when it "rewrites" a URL it does not send a redirect to the client.
Evgeny
A: 

If you're wanting to avoid a redirect (per your comment to Evgeny) the only other option is a server.transfer. With server.transfer the processing is sent to a different page on the server and the client has no idea (there's no round trip back to the client between pages).

Aside from server.transfer actual redirects are quite typical ISAPI_Rewrite is a popular tool and works really well as Evgeny mentioned.

Ian Suttle
A: 

UPDATE: This will do a redirect, not reset the variable. I won't delete this answer as I think it's neat but it doesn't answer exactly what you want

I do this in a very simple way for Economist.com

You have two IIS sites for one site, they share the same webroot. The master IIS web site stays as is listening on the current host header e.g.

216.35.68.215 economist.com

and the other IIS site listens on the ip address with the other host headers entered.

i.e.

216.35.68.215 economist.co.uk

The second web slave in the "Home Directory" has a redirect set which will correct whatever host name people arrive at your site on

Set "The exact URL entered above" and "A permanent redirection for this resource" and enter your fully qualified host name entry as follows

http://www.economist.com$S$Q

The $S$Q will maintain all URLs on your other host names that enter.

Feel free to test this

http://www.economist.co.uk/world/americas/

will just push you to

http://www.economist.com/world/americas/

This means you don't have to run, at least for this reason, the fairly expensive IIS rewrite module

Stewart Robinson
+1  A: 

Yes, it is possible to use the IIS 7 Rewrite Module. You can use the GUI to setup a redirect in IIS7 per this post.
Also you can use the command line per this answer.
In both these examples you have to have a separate website setup with its own home directory and bindings to the domains you want redirected. I hope this makes sense.

notandy
+1  A: 

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]
Cheeso
+1  A: 

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.

RuslanY