Why isn't this RewriteRule working?

Topics: User Forum
Apr 13, 2007 at 5:28 PM
Goal: rewrite /Recipes/Recipe/12345 to /Recipes/Recipe.aspx?ID=12345

RewriteRule ^/Recipes/Recipe/(0-9+)$ /Recipes/Recipe.aspx?ID=$1 L,I

Log shows this when IIS fires up:
Fri Apr 13 12:21:07 2007 - Read .ini file: setting LogLevel to 5
Fri Apr 13 12:21:07 2007 - ini line 3: RewriteRule 1 (null) (null) (null)
Fri Apr 13 12:21:07 2007 - not a duplicate rule...

And none of my rules load. What's wrong with my rule?
Apr 13, 2007 at 7:52 PM
I don't think it's your rule. I tested it in Expresso (great little freeware RexEx app!) and it pans out. I also ran it with testdriver.exe and it rewrote the rule properly.

Maybe you don't have IR setup right?
Apr 16, 2007 at 1:19 PM
I have other rules in the file that work great, but only when the one above is excluded. If I put this one in the rule file anywhere, the whole thing fails.
Apr 16, 2007 at 5:39 PM
Maybe posting your INI file may be of some help in diagnosing what's going on.

However, the "L,I" you have at the end of the rule states that if this rule is satisfied, stop processing any other rules and ignore case.

So is it possible you have other similar rules and since this one is satisfying a request it's not processing any other rules?

I highly recommend you test all your rules with testdriver.exe and the sampleurls.ini. it's the quickest way to know what's going on.
Apr 16, 2007 at 6:58 PM
This is my complete rules file. I ran testdriver against the rules below and it crashes immediately:

RewriteLog c:\temp\iirfLog.out
RewriteLogLevel 5
RewriteRule ^/Recipes/Recipe/(0-9+)$ /Recipes/Recipe.aspx?ID=$1 L,I
RewriteRule (.*)/$ $1/Default.aspx L,I
RewriteRule ^(/A-Za-z0-9-_/*^/)$ $1.aspx L,I

Apr 16, 2007 at 8:50 PM
I ran testdriver with your rules on my side and it didn't bomb on me.

I am curious as to the 2nd rule
RewriteRule (.*)/$ $1/Default.aspx L,I

1. you don't have a starting carrot "^"
2. you're saying "any character and any number of repetitions" with a trailing "/" goes to $1/default.asp

Not sure I understand that logic.

I'd probably write it such as:
RewriteRule ^/(.*)/$ /$1/default.aspx L,I

Basically it says anything typed after the root of the site with a trailing "/" rewrite it to "/something/default.aspx" and stop processing more rules and ignore case.

Typically you don't need to put the default.aspx as IIS automatically loads "default.aspx" as a root page of a folder. But that's up to you.

The 3rd rule is almost the same as the 2nd. Your saying "anything with letters, numbers, dash, underscore, and a forward slash with any number of repetitions but any other character other than a forward slash "^/" rewrite to $1.aspx.

Sounds kind of silly to me.

Maybe you can explain what you're trying to accomplish with the 2nd and 3rd rules.

I'd also recommend downloading Expresso (free regex builder, tester, analyzer)
Apr 17, 2007 at 1:47 PM
I had some tabs in my rule file that I've removed and that's seemingly resolved the issues I was having.

Here's the additional info on our setup. We had to map all files to go through IIS for our CMS system. This disables IIS default docs so we handle it with IIRF.

The second and third rules combine to allow hiding all .aspx extensions. The third rule also ignores all other file types (or filenames with any non-alphanumeric characters) but we have total control over URLs and page names to fit the rules.

/ -> /Default.aspx
/Shop/ -> /Shop/Default.aspx
/Shop/Start -> /Shop/Start.aspx

Your updated second rule doesn't handle the root request (i.e. http://domain.com/), only requests with at least one subdirectory. I removed the first / in your rule to fix that. Everything appears to be working as desired now!

Thanks for your help!
Apr 25, 2007 at 4:08 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.