简体   繁体   English

IIS 7中URL的域名重写

[英]URL Rewrite of domain name in IIS 7

I have two asp.net sites in IIS, api.mydomain.com and mobileapi.mydomain.com. 我在IIS中有两个asp.net网站,api.mydomain.com和mobileapi.mydomain.com。 and some of the requests to api should be rewritten to mobileapi. 并且对api的某些请求应重写为mobileapi。 I cant get it to work. 我无法正常工作。 From my experiments it seems like I cant rewrite the domain name part of the url with rewrite. 从我的实验看来,我无法用重写来重写URL的域名部分。 If I change to redirect everthing works (except that I want a rewrite and not redirect) 如果我更改为重定向,那么一切正常(除了我想要重写而不是重定向)

However, if I instead redirect to something on the same domain, lets say I swap the action url to /testing/{R:0} (without any domain name) then it works as expected and rewrites just fine. 但是,如果我改为重定向到同一域上的某个内容,可以说我将操作URL交换到/testing/{R:0} (不带任何域名),则它可以按预期工作并重写就可以了。

My web.config 我的web.config

<modules runAllManagedModulesForAllRequests="true">
</modules>
<rewrite>
  <rules>
    <rule name="Proxy mobile API" stopProcessing="true">
      <match url=".*" />
      <conditions>
        <add input="{REQUEST_URI}" pattern="([^/]+)" />
        <add input="{StaticRewrites:{C:0}}" pattern="(.+)" />
        <add input="{HTTP_HOST}" pattern="^api.mydomain.se$" />
      </conditions>
      <action type="Rewrite" url="http://mobileapi.mydomain.se/{R:0}" />
    </rule>
  </rules>

  <rewriteMaps>
    <rewriteMap name="StaticRewrites">
      <add key="mobile" value=" " />
      <add key="scripts" value=" " />
    </rewriteMap>
  </rewriteMaps>
</rewrite>  

Depending on the project type you have setup, but if you are using MVC 4 you can simply make this switch in the Global.asax.cs file. 根据您所设置的项目类型,但是如果您使用的是MVC 4,则只需在Global.asax.cs文件中进行此切换即可。

protected void Application_BeginRequest(){
   //mobile device detection and redirection
     if (Request.Browser["IsMobileDevice"] == "true"){
          Response.Redirect("http://mobileapi.mydomain.se");
         }
    }

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

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