简体   繁体   English

IIS7将url子文件夹重写到父文件夹(根)

[英]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 我有一个网站mywebsite.com,并将cms安装到一个子文件夹中: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 现在,我需要将cms移到父文件夹的根目录,并且我希望URL可以自动重写,因此在浏览mywebsite.com/subfolder/page1.php时,您将重定向到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). 使用IIS7重写模块很容易,并且我将针对我认为您要问的问题提供两个不同的示例。

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 - 因此,如果您想“重写”子文件夹中的url,但看起来好像它们位于根目录中,则可以使用以下命令-

<?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 - 但是,如果您希望重定向旧文件夹中的网址(例如,已被Google索引的网址)(作为永久301重定向),则可以使用以下示例-

<?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 - 或者,如果像我一样,您在该页面上绊倒了,想重写一个子文件夹,然后执行从旧子文件夹到根目录的永久性301重定向,则可能需要类似于以下内容的内容-

<?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>

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

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