IIS 7.5 URL Rewrite - Rewrite a Folder from an URL

风格不统一 提交于 2019-12-21 07:55:21

问题


I use IIS 7.5 and URL Rewrite.

I have a website with the following file hierarchy:

webroot
webroot/LegacySite

Both webroot/ and legacy/ are separate App-Folders in IIS.


I need to rewrite my URLs so:

  • If a request is http://mysite.co/LegacySite/page.aspx the URL will be rewritten to http://mysite.co/page.aspx

Below my Web.Conf (in the webroot folder) does not work properly, could you point out what I'm missing?

<?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="MyRole" stopProcessing="true">
                        <match url=".*" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^mysite.com" />
                            <add input="{PATH_INFO}" pattern="^\LegacySite\" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="\LegacySite\{R:0}" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>

回答1:


The following should work:

<rule name="MyRole" stopProcessing="true">
    <match url="LegacySite/(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^mysite.com$" />
    </conditions>
    <action type="Rewrite" url="/{R:1}" appendQueryString="true" />
</rule>

You might want to drop the conditional for checking the host name. Is that really important? Do you have any other domain names bound to that website for which you don't want the redirect to happen? It seems unnecessary. You probably only need:

<rule name="MyRole" stopProcessing="true">
    <match url="LegacySite/(.*)" />
    <action type="Rewrite" url="/{R:1}" appendQueryString="true" />
</rule>

I've added appendQueryString="true" to pass any (optional) query string parameters to the rewritten URL.



来源:https://stackoverflow.com/questions/10615572/iis-7-5-url-rewrite-rewrite-a-folder-from-an-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!