简体   繁体   中英

Adding a “fake” root folder using rewrite

I'm not sure where to go on this one. I've got rewrites to remove file extensions and such but what I can't seem to find out how to do is to add a "fake" root directory to the site. For example if the site is www.foo.com/index.htm I'd like to rewrite the URL to show www.foo.com/root/index.htm. I can use either the IIS rewrite module or mod rewrite I'm just uncertain on how to go about this (or if it's even possible) and my google-fu has failed me.

If I understand it correctly, you want requests to come with root prefix. In that case this rewrite will do it:

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

To modify the html served to the client outbound rule is required:

<outboundRules>
    <rule name="FakeRoot" preCondition="IsHtml">
        <match filterByTags="A, Area, Base, Form, Frame, IFrame, Img, Input, Link, Script" pattern="http://www\.foo\.com/(.*)" />
        <action type="Rewrite" value="http://www.foo.com/root/{R:1}" />
    </rule>
    <preConditions>
        <preCondition name="IsHtml">
            <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
        </preCondition>
    </preConditions>
</outboundRules>

It assumes that you have fully qualified URL in tag attributes, if relative links are used you need more sophisticated rewrites.

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