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,
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.
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]
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With