Rewriting .aspx to .asp files (or .aspx to .html file)

Sep 19, 2009 at 8:05 PM
Edited Sep 19, 2009 at 8:08 PM

I reinstalled IIS, and reinstalled the filter since I'd messed up my IIS anyway.

My site is http://localhost:8002/testpage.aspx

I'm trying to rewrite it to http://localhost:8002/testpage or http://localhost:8002/testpage.asp or http://localhost:8002/testpage.htm

This is my current IsapiRewrite4.ini from the site:

RewriteRule ^/.html$ /$1.aspx$3 [L]

I get a 404 error, what should I do to fix this?

I'm on IIS 7.0 on Windows Vista Home Edition by the way.

Thanks

Coordinator
Sep 21, 2009 at 8:11 PM

Your regex is not matching the incoming URLs.  you need something closer to this

RewriteRule ^/(.+)\.html$ /$1.aspx   [L]

If you want to match .htm or .html on the incoming side, then change it to this:

RewriteRule ^/(.+)\.htm(l)?$ /$1.aspx   [L]

or this, if you think it is more readable:

RewriteRule ^/(.+)\.(htm|html)$ /$1.aspx   [L]

If you want to append the query string automatically, then change it to this:

RewriteRule ^/(.+)\.(htm|html) /$1.aspx   [L,QSA]

I don't know what that $3 was doing in your replacement string. I'm guessing it was left over from some previous attempts to get it working. in your original rule, there wasn't even a single capture, so even $1 would evaluate to nothing, had the rule matched. And the rule would match only a url like this: ".html" Any URL with something preceding the .html would not match. "Hello.html" would not match. In my rules the (.+) captures a string of one or more characters.

This is all written up in the doc in much greater detail, with lots of examples. It really is a good idea to read the documentation.