如何将域的所有请求路由到子文件夹asp.net

时间:2013-06-12 20:25:57

标签: asp.net asp.net-mvc-4

我已经看到了很多这方面的解决方案,但似乎没有工作......

我有一个域名

producerpte.co.uk

但我在子文件夹中设置了一个单独的应用程序,以便查看您必须访问的应用程序的根目录...

producerpte.co.uk/producerpte

但我希望它能让用户看不到网址的“site1”部分。

当我在Web.Server配置中使用url重写或在代码中使用Context.RewritePath时(在根目录中设置的应用程序中),它只是将用户重定向到url(其中包含site1)I不希望用户知道他们要访问子文件夹。

我说错了吗?

我实际上正在使用Winhost。我试过的所有例子都不会在不重定向网址的情况下更改通话结果: - (

我被winhost告知我应该用代码/配置来做。我更愿意用代码来实现它......

更新:我尝试了Max的答案,我做了......

 <rules>
    <rule name="Redirect if producerpte" stopProcessing="true">
        <match url="^ producerpte/(.*)$" />
        <action type="Redirect" url="{R:1}" />
    </rule>
    <rule name="Rewrite to sub folder">
        <match url="^.*$" />
        <action type="Rewrite" url="producerpte/{R:0}" />
    </rule>
  </rules>

但没有运气:-(而且网址中的网址仍然有变化......

1 个答案:

答案 0 :(得分:3)

如果WinHost确实启用了IIS重写模块,请编辑您的web.config文件并尝试使用此规则:

<rewrite>
<rules>
    <rule name="Redirect if site1" stopProcessing="true">
        <match url="^site1/(.*)$" />
        <action type="Redirect" url="{R:1}" />
    </rule>
    <rule name="Rewrite to sub folder">
        <match url="^.*$" />
        <action type="Rewrite" url="site1/{R:0}" />
    </rule>
</rules>
</rewrite>

---更新---

好吧,我测试了这个IIS配置:

enter image description here

我在 root 网站上添加了规则 IIS创建了一个web.config文件:

enter image description here

以下是web.config文件的内容:

<configuration>
    <system.webServer>
        <rewrite>
             <rules>
                <rule name="Redirect if producerpte" stopProcessing="true">
                    <match url="^producerpte/(.*)$" />
                    <action type="Redirect" url="{R:1}" />
                </rule>
                <rule name="Rewrite to sub folder">
                    <match url="^.*$" />
                    <action type="Rewrite" url="producerpte/{R:0}" />
                </rule>
              </rules>
        </rewrite>
    </system.webServer>
</configuration>
  • http://localhost:8066/在不更改浏览器中的网址的情况下显示C:\inetpub\wwwroot\producerpte\producerpte的内容

  • http://localhost:8066/producerpte/显示C:\inetpub\wwwroot\producerpte\producerpte的内容,但浏览器中的网址已更改为http://localhost:8066/

请确保您在 root 网站(而不是子应用程序)上创建了规则,并且我注意到您的配置中有一个领先的空间:<match url="^ producerpte/(.*)$" /> {{1}之间}和^

相关问题