natiz
natiz

Reputation: 573

ASP.NET: How to remove the file name.aspx from URLs

I'm trying to write a rewrite rule for web.config file so that the url will remove the file name from it, meaning:

http://www.example.com/admin/Index.aspx
http://www.example.com/admin/Product.aspx

will become:

http://www.example.com/admin/
http://www.example.com/admin/

AND:

http://www.example.com/Index.aspx
http://www.example.com/Product.aspx

to:

http://www.example.com/
http://www.example.com/Product/

tried a couple of codes found online, but all of them gave me internal error.

Thanks in advance!

Upvotes: 2

Views: 3596

Answers (2)

I have used the open source UrlRewriter to do that on a number of projects, it works very well

http://urlrewriter.net/

In order to make it work you must add it to your web.config file, like so:

<httpModules>
    <add type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" name="UrlRewriter" />
</httpModules>

And add a routing section as well, they have good documentation, but basically you provide rewrite-rules, that could look something like this:

<rewriter>    
    <rewrite url="~/([a-zA-Z0-9]+)$" to="~/$1.aspx" />
</rewriter>

Add the UrlRewriter dll-files to your bin, and set IIS to handle wildcards:

  1. Find site in IIS Manager (inetmgr)
  2. Open properties on your site
  3. Go to tab "Home Directory"
  4. Click "Configuration" under Application Settings
  5. Click "Insert" under "Wildcard application maps"
  6. Put path to .NET executable (same as the one used for handling .aspx files) in the "Executable" field
  7. Uncheck "Verify file exists"
  8. Press "OK"

And you are done :)

It's quite a good solution for WebForms projects, but make sure you test it well, and verify that GoogleBot can still access your site.

Upvotes: 4

stack72
stack72

Reputation: 8288

im not sure if you have it but with IIS7 there is a module that you can install that you can set up url rewriting rules. It will add the appropriate rules to the web.config of your site

a guide by Scott Guthrie of Microsoft may help - http://weblogs.asp.net/scottgu/archive/2010/04/20/tip-trick-fix-common-seo-problems-using-the-url-rewrite-extension.aspx

Upvotes: 0

Related Questions