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

28,024

Solution 1

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.

Solution 2

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.

Solution 3

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]

Solution 4

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.

Solution 5

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.

Share:
28,024
SharePoint Newbie
Author by

SharePoint Newbie

Updated on July 09, 2022

Comments

  • SharePoint Newbie
    SharePoint Newbie almost 2 years

    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,

  • SharePoint Newbie
    SharePoint Newbie about 15 years
    I don't want to doa redirect. Instead I want to change the {HTTP_HOST} for the current request.
  • EMP
    EMP about 15 years
    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.
  • notandy
    notandy over 11 years
    Looks like it moved to trainsignal.com/blog/…