41

In a hosted IIS7 environment, I am looking for the simplest way to use extension-less file names. Simply I have the following pages:

  • index.html (or .aspx) --> example.com
  • gallery.html --> example.com/gallery
  • videos.html --> example.com/videos
  • etc...

I only have a handful of pages, I have no dynamic code, nothing special. All the examples I have found or methods I use in other sites I've developed revolve around dynamic content, pages, etc. I am simply looking for the simplest solution, ideally not requiring any sort of URL rewrite module installed. Preferably, I could keep the .html extension instead of converting the site to a ASP.NET project, but that is an option.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
roadsunknown
  • 3,190
  • 6
  • 30
  • 36
  • Refer to this answer it Works Well [Remove .aspx form all Pages][1] [1]: http://stackoverflow.com/a/15240694/1265997 – cwishva Mar 06 '13 at 06:47
  • please visit this link http://stackoverflow.com/questions/2170745/what-are-pros-to-use-extension-less-url – Mohamed Kamal Dec 19 '10 at 06:11

7 Answers7

57

I ended up using the following sites:

http://blogs.msdn.com/b/carlosag/archive/2008/09/02/iis7urlrewriteseo.aspx

and

http://forums.iis.net/t/1162450.aspx

or basically the following code in my web.config file using the IIS7 URL Rewrite Module that most hosted sites now offer (in this case I am using GoDaddy):

<system.webServer>
    <rewrite>
        <rules>
            <rule name="RewriteASPX">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="{R:1}.aspx" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>
Turnip
  • 35,836
  • 15
  • 89
  • 111
roadsunknown
  • 3,190
  • 6
  • 30
  • 36
14

Another little bit more modern way to do this is using Microsoft.AspNet.FriendlyUrls. In the Global.asax.cs add:

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    RouteConfig.RegisterRoutes(RouteTable.Routes);

and in the RouteConfig file

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings);
    }
  • 1
    Best one , it is **officially supported** now. When you create new `WebForms` project , this code is automatically added. – Shaiju T Jul 11 '18 at 09:45
7

Another simplest solution for achieving the same:

Put following lines of code into your global.ascx file:

void Application_BeginRequest(object sender, EventArgs e)
{
   String fullOrigionalpath = Request.Url.ToString();
   String[] sElements = fullOrigionalpath.Split('/');
   String[] sFilePath = sElements[sElements.Length - 1].Split('.');

   if (!fullOrigionalpath.Contains(".aspx") && sFilePath.Length == 1)
   {
       if (!string.IsNullOrEmpty(sFilePath[0].Trim()))
           Context.RewritePath(sFilePath[0] + ".aspx");
   }
}
Pawan Maheshwari
  • 15,088
  • 1
  • 48
  • 50
  • pawan i have addded ur code but still seeing page name with extension in url, – Nikhil D Jun 12 '13 at 07:53
  • It should work, I have used this on couple of my projects. Re-check if something else is missing. – Pawan Maheshwari Sep 11 '13 at 06:32
  • 3
    This code allows page to work without .aspx , but it does not deletes .aspx – Augis Jan 24 '14 at 15:15
  • So, how to delete the .aspx extension? as Augis has rightly pointed out it just works without the .aspx extension, but does not remove it. How should i go about doing the same? – Jaya Apr 08 '14 at 00:00
1

If you have dynamic code, I think that the easiest thing to do is to just rename the files from .aspx to .html especially if you only have a handful of pages. There is no simple way to do it without rewriting the URL somehow.

However, with IIS 7, you can set it up really easily with an HTTP Module. Scott Guthrie explains this really well. In this post, he shows several approaches to customizing the URLs. I think that you would like approach #3 the best.

http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

dontangg
  • 4,729
  • 1
  • 26
  • 38
  • Thanks, but the idea is to get rid of extensions completely. Not rename them from HTML > ASPX or vice versa. The ScottGu post is a little outdated, but does have some useful, however more complex than I am looking for information. – roadsunknown Dec 29 '10 at 09:39
  • The information provided in that link does tell you how to remove the extension if you look at approach 3. The instructions there describe the _current_ process to remove extensions with IIS7 even though it was written in 2007. – dontangg Dec 29 '10 at 15:28
0

I don't have enough points to comment, and this is improving Pawan M's answer. His will work, unless you have Query Strings being used on the page. I have modified Pawan's code to allow for query strings, not to mention mine is the vb version.

Check to make sure your project has a Global.asax.vb file in it. If it doesn't Add an Item by doing this:

File -> New -> File -> Global Application Class

In the Global.asax file of your project add this function:

Sub Application_BeginRequest(sender As Object, e As EventArgs)
    Dim fullOrigionalpath As [String] = Request.Url.ToString()
    Dim sElements As [String]() = fullOrigionalpath.Split("/"c)
    Dim sFilePath As [String]() = sElements(sElements.Length - 1).Split("."c)
    Dim queryString As [String]() = sElements(sElements.Length - 1).Split("?"c)

    If Not fullOrigionalpath.Contains(".aspx") AndAlso sFilePath.Length = 1 Then
        If Not String.IsNullOrEmpty(sFilePath(0).Trim()) Then
            If queryString.Length = 1 Then
                Context.RewritePath(sFilePath(0) + ".aspx")
            Else
                Context.RewritePath(queryString(0) + ".aspx?" + queryString(1))
            End If

        End If
    End If
End Sub
sec0ndHand
  • 430
  • 4
  • 8
0

You can do this in c# to use a customized extension in your URL in ASP.NET.

Let ".recon" in the code be your customized extension. (i.e replace ".recon" to your own extension)

protected void Application_BeginRequest(object sender, EventArgs e)
 {
        HttpApplication app = sender as HttpApplication;
        if (app.Request.Path.ToLower().IndexOf(".recon") > 0)
        {
            string rawpath = app.Request.Path;
            string path = rawpath.Substring(0, rawpath.IndexOf(".recon"));
            app.Context.RewritePath(path+".aspx");
        }
 }
-1

Easy Solution For Removing .aspx Extension in asp WebForm for Specific Page:

Check Solution Explorer >> Got to Global.asax File >> Check Method Application_BeginRequest Write this code inside this method

// Remove aspx Extension From Smy Page
string CurrentPath = Request.Path; // getting Current Url
if(CurrentPath == "/YourPageURL")
    HttpContext MyContext = HttpContext.Current;
    MyContext.RewritePath("/YourPageURL.aspx");
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Ghazi Hur
  • 111
  • 1
  • 2