1
votes

I just installed IIS Url Rewrite 2.0 on my IIS Server and I'm having a bit of trouble getting some of my rewrites correctly executed. Here's my trouble:

I want all my files to be rewrittin to: welcome.aspx > welcome/, about.aspx > about/ etc. to achieve that I'm doing this:

<rule name="Remove extension with slash" stopProcessing="true">
    <match url="(.*)/" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}.aspx" appendQueryString="true" />
</rule>

So far that's working.. but I also have some querystring functions on default.aspx I want to be executed, when I run a URL like this: mysite.com/logout/ which really is mysite.com/default.aspx?action=logout due to this rule:

<rule name="Friendly URL" stopProcessing="true">
    <match url="(.*)/" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="default.aspx?action={R:1}" appendQueryString="true" />
</rule>

But it wont execute the second rule because it will look for the logout.aspx file which does not exist, only in form of a querystring function in my default.aspx

Any way I can achieve both?

::::::::::: EDITS ::::::::::::

Ive been trying all day now.. I got pretty close with the following setup:

<rule name="Remove ext" stopProcessing="true">
    <match url="^([^/]+)/?$" />
    <conditions>
        <add input="{REQUEST_FILENAME}.aspx" matchType="IsFile" negate="false" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}.aspx" appendQueryString="true" />
</rule>

<rule name="Default actions" stopProcessing="true">
    <match url="^([^/]+)/?$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="default.aspx?action={R:1}" appendQueryString="true" />
</rule>

<rule name="Other files with querystrings" stopProcessing="true">
    <match url="^([^/]+)/([^/]+)/?$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}.aspx?action={R:2}" appendQueryString="true" />
</rule>

Almost everything works

user.aspx > user

default.aspx?action=logout > logout

user.aspx?action=comments > user/comments

Only thing messing it up is if I add a slash after user ie. it will respond to the default.aspx rule and act as a querystring.. which is not intented.. Any clue?

1

1 Answers

1
votes

How can you expect any system to distinguish between a something that is supposed to be a page name and something that is meant as a parameter?

What an MVC site would do here - and your URL system is very similar to that - is to clearly define what's what. In your case, you should use the first part (welcome, about) as the view - the page - part and add another level that would represent the method. In short, use /default/logout and rewrite this to something like

{R:1}.aspx?action={R:2}

If you really want to look your URLs like you said (/logout without the /default part), you could either add a rule that rewrites them individually (matched with an OR | ) or add a Logout.aspx that redirects to Default.aspx?action=logout - but that would be ugly. Go for the clean solution.