Hide ASPX url extension

Topics: User Forum
Mar 18, 2011 at 9:30 AM

I would like to hide ASPX intension from my online application URL. For example if someone hits url www.mydomain.com/mypage the url rewrite to www.mydomain.com/mypage.aspx

Could anyone suggest complete rules? 

Mar 19, 2011 at 9:27 PM

The simplest way to start would be (1)

RewriteRule ^/(.*)    /$1.aspx

What this does is rewrite all incoming URL requests to one that includes an extension of .aspx .  

This is probably not quite right though. In particular,

  • The .* matches a sequence of zero or more of anything.  If there are ZERO characters, you probably don't want to rewrite at all.  So instead, use .+, eg  RewriteRule ^/(.+)$   /$1.aspx
  • if someone requests a URL than ends with .aspx, you probably don't want to rewrite to append another .aspx.  Probably.  Maybe you do, maybe you don't, I don't know.

In some cases when users explicitly request a URL with an extension, web administrators want to REDIRECT (not Rewrite) to the corresponding extensionless URL.  At this point, the client (browser) will send a new request, with no extension, and that one can then gets rewritten.  The effect is that all public URLs are extensionless, even though internally the .aspx extension is used. This technique is described in the IIRF documentation and there are examples in the "Example Ini Files" section. It looks basically like this:

RedirectRule ^/(.*).aspx    ^/$1
RewriteRule  ^/(.+)$    /$1.aspx

So if someone requests the url  /Intro ,  it gets rewritten to /Intro.aspx .    If someone explicitly requests /Intro.aspx, it gets redirected to /Intro , which then gets rewritten (as just described).

But once again, things are probably not so simple. In particular, what about query strings?  If someone presents a URL that contains a query string, such as /Intro?page=2 , the rewrite will result in /Intro?page=2.aspx  , which is probably wrong.  To insure that the rules DON'T capture the query string on an extensionless URL, you need to match on "one or more of any character that is not question mark" for the rewrite.  And likewise for the Redirect.  Also you need to apply the QSA to append any query string that is passed. This looks like so:

RedirectRule ^/([^\?]+).aspx(\?|$)    ^/$1    [QSA]
RewriteRule  ^/([^\?]+)(\?|$)            /$1.aspx       [QSA]