Hirthas
Hirthas

Reputation: 359

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.

Upvotes: 0

Views: 333

Answers (1)

Tomek
Tomek

Reputation: 3279

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.

Upvotes: 1

Related Questions