tags:

views:

71

answers:

1

Hey all, I've got a question about IIS7 rewrite.

I'm wondering if there is a way to set conditions for re writing urls. I'm wanting to rewrite:

http://www.domain.com/user.aspx?id=username

to become

http://www.domain.com/username/

I also have

http://www.domain.com/article.aspx http://www.domain.com/login.aspx

and I want those to become

http://www.domain.com/article/ http://www.domain.com/login/

The issue I'm having is that if I set up the rewrites for username the rewrites for article and login break.

I need to somehow exclude those from the username rewriting so that they can be handled on their own.

Here is my current code, im rewriting the usernames to /user/username at the moment:

<rewrite>
  <rules>
    <rule name="Rewrite user accounts2">
      <match url="user/([_0-9a-z-]+)"/>
      <action type="Rewrite" url="user.aspx?id={R:1}"/>
    </rule>
    <rule name="Rewrite user accounts">
      <match url="user/([_0-9a-z-]+)/"/>
      <action type="Rewrite" url="user.aspx?id={R:1}"/>
    </rule>
  </rules>
</rewrite>
A: 

If you add the article/login rule at the top and add stopProcessing="true" to it then you dont have to use conditions.

<rewrite>
  <rules>
    <rule name="Login" stopProcessing="true">
      <match url="login/?"/>
      <action type="Rewrite" url="login.aspx"/>
    </rule>
    <rule name="Article" stopProcessing="true">
      <match url="article/?"/>
      <action type="Rewrite" url="article.aspx"/>
    </rule>
    <rule name="Rewrite user accounts2" stopProcessing="true">
      <match url="user/([_0-9a-z-]+)/?"/>
      <action type="Rewrite" url="user.aspx?id={R:1}"/>
    </rule>
  </rules>
</rewrite>

PS. the questionmark makes the preceding character optional.

Fabian
Thanks so much Fabian, just one more part to the question.Say I want to take away /user/ and just make the account domain.com/username/ how do I stop it conflicting with the login/article rewrites?Matt
bExplosion
You're quite welcome :).You can just remove user/ from the match url in the example i wrote.So when its not login or article it will check "([_0-9a-z-]+)/?" and rewrite accordingly.
Fabian