开发者

Bidirectional URL Rewriting/Redirecting in IIS7.5

First off, I'd like to apologise for the ludicrous title. I'm not trying to sound cool or clever by using the word 'bidirectional', I just genuinely couldn't think of another way to describe it. Promise.

On to my problem. I have the following in the <system.webserver>/<rewrite>/<rules> section of my Web.config.

<!-- Who We Are -->
&l开发者_JAVA技巧t;rule name="1A">
    <match url="^whoweare\.aspx$" />
    <action type="Redirect" url="who-we-are" redirectType="Permanent" />
</rule>
<rule name="1B">
    <match url="^who-we-are$" />
    <action type="Rewrite" url="whoweare.aspx" />
</rule>

<!-- What We Do -->
<rule name="2A">
    <match url="^whatwedo\.aspx$" />
    <action type="Redirect" url="what-we-do" redirectType="Permanent" />
</rule>
<rule name="2B">
    <match url="^what-we-do$" />
    <action type="Rewrite" url="whatwedo.aspx" />
</rule>

Now this works tremendously. Effectively, if you visit the URL http://example.com/whoweare.aspx (which is the actual URL of the page), you'll be 301 redirected to the URL http://example.com/who-we-are (the virtual URL), and if you visit the virtual URL, you'll be rewritten to the actual URL.

This means super sexy URLs without duplication, and it doesn't result in reciprocal rewriting either, so smiles all round.

My question is this: could this be done more elegantly?

It's a little cumbersome having to write out two rules to ensure that one is redirected to the other, and the other is rewritten to the one. Is it possible to write one rule which will achieve the functionality of the above two?


Elegance is a subjective term, I guess there are a couple of ways that would be better, such as using Rewrite extensibility and implement fancy mapping logic, but by far the way I would recommend is using just 2 rules, one for Redirect and one for Rewrite, and with that just leverage Rewrite Maps that will make it a bit more readable (but still painful) to manage them, for example below you would now only need to maintain the maps and never have to deal with rules anymore:

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Rewrite From Pretty URL" stopProcessing="true">
                <match url=".*" />
                <conditions>
                    <add input="{URLsToRewrite:{REQUEST_URI}}" pattern="(.+)" />
                </conditions>
                <action type="Rewrite" url="{C:1}" appendQueryString="false" />
            </rule>
            <rule name="Redirect To Pretty URL" stopProcessing="true">
                <match url=".*" />
                <conditions>
                    <add input="{URLsToRedirect:{REQUEST_URI}}" pattern="(.+)" />
                </conditions>
                <action type="Redirect" url="{C:1}" appendQueryString="false" />
            </rule>
        </rules>
        <rewriteMaps>
            <rewriteMap name="URLsToRewrite">
                <add key="/who-we-are" value="/whoweare.aspx" />
                <add key="/what-we-do" value="/whatwedo.aspx" />
            </rewriteMap>
            <rewriteMap name="URLsToRedirect">
                <add key="/whoweare.aspx" value="/who-we-are" />
                <add key="/whatwedo.aspx" value="/what-we-do" />
            </rewriteMap>
        </rewriteMaps>
    </rewrite>
</system.webServer>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜