Chris
Chris

Reputation: 2646

Removing URL file extension

I have been exploring "prettier" URLs of which the first thing I've done is remove the extension (eg. .aspx). To do this I've used the URL Rewrite module in IIS.

This works so now if I access

http://www.mysite.com/folder/filename

or

http://www.mysite.com/folder/filename.aspx

The latter gets redirected to the first and the page loads. I then tried the following:

http://www.mysite.com/folder/filename/

which breaks (as expected I suppose) but this seems like bad user experience. Maybe as a frequent web user I feel that having no slash or a slash should work. The only way I know to have a slash on the end that works is to create a folder and use the default default.aspx page, which I'd like to avoid if I can.

Continuing, I tried adding a directory with the same name as a file, so in the case of the example above I created a directory called filename. In this directory I created a default default.aspx. Now, if I try and access the same url http://www.mysite.com/folder/filename I am sent to the default.aspx page of that new folder with a slash appended to the URL http://www.mysite.com/folder/filename/.

This suggests that by default URLs without extensions attempt to access directories, and only if not found will IIS overwrite provide a file, so in a sense it seems like a hack to try and fool it with extension-less file names.

I am fortunately in a position where I have full control over the file and folder names, so I can hopefully prevent any accidents there (albeit I will have to be careful). So my question: Is there, a way to remove the file extension but still allow a trailing slash to be appended and still find the file. I don't want to have to create loads of directories all with default.aspx inside, if I can help it.

Upvotes: 2

Views: 2143

Answers (3)

Jaimal Chohan
Jaimal Chohan

Reputation: 8645

IIS Rewrite (correctly) matches URL's with a trailing slash and without a trailing slash as different URL's. Depending on how the rule has been implemented, you may need to

  • implement 2 rules, one to deal with adding (or removing) trailing slashes from all or sepcific URLS and the other to deal with rewriting URL's to .aspx pages.
  • you might need to added separate maps for URL's with and without trailing slashes

The solution really depends on where and how you match.

Upvotes: 0

Antonio Bakula
Antonio Bakula

Reputation: 20693

Easiest way is to use ASP.NET routing, I presume that you are talking about web forms, for your example it's pretty easy:

  1. Add System.Web.Routing reference to the project
  2. Use routing in Global.asax.cs (using System.Web.Routing;)
  3. Add routes in application start :
protected void Application_Start(object sender, EventArgs e)
{
  RouteTable.Routes.MapPageRoute("", "Filename", "~/Filename.aspx");
}

and that's it.

Here is a more information about Routing for win forms :

http://msdn.microsoft.com/en-us/library/cc668201.aspx#adding_routes_to_a_web_forms_application

http://msdn.microsoft.com/en-us/library/dd329551.aspx

Upvotes: 1

Philip Moore
Philip Moore

Reputation: 11

I'm not completely sure how IIS handles mod_rewrite but hopefully the logic that i apply in a linux environment will help you solve the problem.

In my .htaccess file i first write two conditions that will apply to the following rules; it says that if the following url is not a file and is not a directory redirect the user to the following page.

However if the directory or the file exists that will always take precedence.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^news/([^/]*)$ news.php?article_slug=$1 [L,QSA]

The rule allows for the structure you require (folder/file) or at least simulates it as a url.

So the above example will direct

http://www.mysite.com/news/article-name & http://www.mysite.com/news/article-name/

to news.php with a GET variable article_slug

Let me know if thats of any help or if i can help further.

Upvotes: 1

Related Questions