简体   繁体   中英

IIS7 rewrite url subfolder to parent folder (root)

I have a website mywebsite.com and installed a cms into a sub folder: mywebsite.com/subfolder

I need to now move the cms to parent folder root and I would like url's to be rewritten automatically so when browsing mywebsite.com/subfolder/page1.php you get redirected to mywebsite.com/page1.php

so far the only suggestions I found were redirecting any request to the root folder. In my case I need to rewrite.

Thanks!

I guess the reason why your question is so old and has not been answered is because it is not clear what you are asking for. Using the IIS7 rewrite module is easy, and I will provide a couple of different examples for what I think you are asking (either or).

So if you would like to "rewrite" urls that are in a subfolder but have them appear as if they were in the root, the following would work -

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Root_URL_Rewrite" stopProcessing="true">
                    <match url="^(.*)" />
                    <action type="Rewrite" url="/subfolder/{R:0}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

If however you would like url's in an old folder (say url's that have been indexed by google) to be redirected (as a permanent 301 redirect) you could use the following example -

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Redirect_Root_URL" stopProcessing="true">
                    <match url="(.*)subfolder(.*)" ignoreCase="true" />
                    <action type="Redirect" url="{R:2}" redirectType="Permanent" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Or, if like me you stumpled across this page and are wanting to do a rewrite to a subfolder, and then do a permanent 301 redirect from the old subfolder to the root, you might want something more similar to the following -

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
               <rule name="Redirect_Root_URL" stopProcessing="true">
                   <match url="(.*)subfolder(.*)" ignoreCase="true" />
                   <action type="Redirect" url="{R:2}" redirectType="Permanent" />
               </rule>
                <rule name="Root_URL_Rewrite" stopProcessing="true">
                    <match url="^(.*)" />
                    <conditions>
                        <add input="{URL}" pattern="^/subfolder/.*" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/subfolder/{R:0}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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