简体   繁体   English

如何在web.config中重写和/或重定向URL

[英]How to rewrite and/or redirect a url in web.config

Here is my problem. 这是我的问题。 I need two things: 我需要两件事:

I need to setup a sub domain (sub.example.com) to load its contents from http://example.com/somepath but the URL needs to remain (sub.example.com) for the user. 我需要设置一个子域(sub.example.com)以从http://example.com/somepath加载其内容,但是该URL需要为用户保留(sub.example.com) This would make http://example.com/somepath/someotherpath equivalent to sub.example.com/someotherpath and so on... 这将使http://example.com/somepath/someotherpath等同于sub.example.com/someotherpath ,依此类推...

And also if the user accesses http://example.com/somepath directly, I either need to redirect him to the sub domain or just rewrite the URL. 而且,如果用户直接访问http://example.com/somepath ,我要么需要将其重定向到子域,要么只需重写URL。

I am using an MVC web application hosted on Azure. 我正在使用Azure上托管的MVC Web应用程序。

I am not good writing rewrite rules. 我写重写规则不好。 So if someone can help me out, if this is possible and what rewrite rules I need to input into my web.config file to achieve this. 因此,如果有人可以帮助我,如果可能的话,需要什么重写规则才能输入到我的web.config文件中以实现此目的。 Or maybe there is a better way. 也许有更好的方法。

Thanks. 谢谢。

Could you try with this ? 你可以尝试一下吗?

<rewrite>
  <rules>
    <rule name="Redirect" stopProcessing="true">
      <match url="(.*)" />
      <action type="Redirect" url="http://sub.example.com/{REQUEST_URI}" redirectType="Found" />
    </rule>
  </rules>
</rewrite>

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

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