Bidirectional URL Rewriting/Redirecting in IIS7.5

Posted by David Foster on Stack Overflow See other posts from Stack Overflow or by David Foster
Published on 2010-05-19T09:57:05Z Indexed on 2010/05/19 10:00 UTC
Read the original article Hit count: 609

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about iis7