views:

312

answers:

1

A really easy one, but I need to get this right, and cannot afford mistakes as I need to deploy on a live server as soon as I can.

http://www.foo.com/bar --> http://www.foo.com/bar.aspx
http://www.foo.com/bar?q=boo --> http://www.foo.com/bar.aspx?q=boo

# I only want /bar to get rewritten to /bar.aspx
# everything else stays as is
http://www.foo.com/bar.aspx --> http://www.foo.com/bar.aspx
http://www.foo.com/bar.pdf --> http://www.foo.com/bar.pdf

I got here, but this turns bar.aspx into bar.aspx.aspx, and that ain't good.

# Helicon ISAPI_Rewrite configuration file

RewriteEngine on
RewriteRule (.*) $1.aspx [L]
+2  A: 

Try this rule:

RewriteCond $1 !.*\.aspx$
RewriteRule (.*) $1.aspx [L]

That should avoid any possible problems with recursion. And if you want to exclude already existing files, try this condition instead:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.aspx [L]
Gumbo
Awesome! What does this say in english "RewriteCond $1 !.*\.aspx$"
DrG
@DrG: `$1` is like in the replacement expression the match of the first group of the `RewriteRule` pattern. And that’s then tested against the pattern `!.*\.aspx$` that simply inverts the result of `.*\.aspx$`. So if the current URL path is `/bar.aspx`, `$1` is `bar.aspx` that is then matched by `.*\.aspx$`, so the result of the `RewriteCond` is *false* since `.*\.aspx$` is negated. That means the rule is not applied on `/bar.aspx` since it ends with `.aspx`.
Gumbo
what would happen to `foo.com/bar.pdf`. I want that to get left alone too. Maybe I actually need your second rule. It's beginning to sink in! I feel like I might need a combination of both. Thanks for your help Gumbo, I'm on much better footing than before :D
DrG
@DrG: You could make your rule more specific by specifying the allowed characters. If, for example, you don’t want to have that rule applied on URLs that already contains a dot (like in a file name), use `^([^.]+)$` instead of the universal `(.*)`.
Gumbo