简体   繁体   English

使用 IIS 6/7 重写 URL - 重写主机名 {HTTP_HOST}

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

I need to rewrite the "Host Name" {HTTP_HOST} for an incoming request.我需要为传入的请求重写“主机名”{HTTP_HOST}。 Is it possible to do this using IIS 7 Rewrite Module?是否可以使用 IIS 7 重写模块来做到这一点?
I want to rewrite http://abc.xyz.com/ * to http://xyz.com/sites/abc/ *.我想将http://abc.xyz.com/*重写为http://xyz.com/sites/abc/ *。 This is being done for a SharePoint site which uses the {HTTP_HOST} internally.这是为内部使用 {HTTP_HOST} 的 SharePoint 网站完成的。

Are there any Url Rewriters out there which let me change the {HTTP_HOST} variable of IIS?是否有任何 Url Rewriters 可以让我更改 IIS 的 {HTTP_HOST} 变量?

Kind regards,亲切的问候,

You can use IIS 7 URL Rewrite Module 2.0 to change the HTTP_HOST server variable.您可以使用 IIS 7 URL Rewrite Module 2.0 来更改 HTTP_HOST 服务器变量。 More information on how to do it is available in Setting HTTP request headers and IIS server variables .有关如何执行此操作的更多信息,请参阅设置 HTTP 请求标头和 IIS 服务器变量

IIRF is free, and lets you rewrite headers, including HTTP_HOST. IIRF是免费的,并允许您重写标头,包括 HTTP_HOST。

I want to rewrite http://abc.xyz.com/ * to http://xyz.com/sites/abc/ *.我想将http://abc.xyz.com/*重写为http://xyz.com/sites/abc/ *。 This is being done for a SharePoint site which uses the {HTTP_HOST} internally.这是为内部使用 {HTTP_HOST} 的 SharePoint 网站完成的。

You need to rewrite both the Host and the Url.您需要重写 Host 和 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.您也可以通配 abc 部分。 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.我不熟悉 IIS 7 重写模块,但ISAPI_Rewrite几乎可以更改您想要的任何 HTTP 标头。 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.是的,可以使用 IIS 7 重写模块。 You can use the GUI to setup a redirect in IIS7 per this blog post .您可以根据这篇博文使用 GUI 在 IIS7 中设置重定向。

If you're wanting to avoid a redirect (per your comment to Evgeny) the only other option is a server.transfer.如果您想避免重定向(根据您对 Evgeny 的评论),唯一的其他选择是 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).使用 server.transfer 处理被发送到服务器上的不同页面,客户端不知道(页面之间没有返回到客户端的往返)。

Aside from server.transfer actual redirects are quite typical ISAPI_Rewrite is a popular tool and works really well as Evgeny mentioned.除了 server.transfer 实际重定向是非常典型的 ISAPI_Rewrite 是一个流行的工具,并且如 Evgeny 提到的那样工作得非常好。

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我以一种非常简单的方式为 Economist.com 执行此操作

You have two IIS sites for one site, they share the same webroot.一个站点有两个 IIS 站点,它们共享同一个 webroot。 The master IIS web site stays as is listening on the current host header eg主 IIS 网站保持原样侦听当前主机标头,例如

216.35.68.215 economist.com

and the other IIS site listens on the ip address with the other host headers entered.另一个 IIS 站点在输入其他主机标头的情况下侦听 ip 地址。

ie IE

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 “主目录”中的第二个 web 从站有一个重定向集,它将纠正人们到达您网站的任何主机名

Set "The exact URL entered above" and "A permanent redirection for this resource" and enter your fully qualified host name entry as follows设置“上面输入的确切 URL”和“此资源的永久重定向”并输入您的完全限定主机名条目,如下所示

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

The $S$Q will maintain all URLs on your other host names that enter. $S$Q 将维护您输入的其他主机名上的所有 URL。

Feel free to test this随意测试这个

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

will just push you to只会把你推到

http://www.economist.com/world/americas/ 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这意味着您不必运行(至少出于这个原因)相当昂贵的 IIS 重写模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM