We are excited to announce that the IIS.NET Forums are moving to the new Microsoft Q&A experience. Learn more >

removing .aspx extentions from url'sRSS

17 replies

Last post Sep 01, 2016 08:55 AM by Umesh Maurya

  • removing .aspx extentions from url's

    Jul 10, 2008 02:11 PM|Mr E|LINK

     hi guys,

    I must say how glad I'm am that there is finally a built-in url rewriter, I've already transformed a horrible query string into something that is much easier on the eye.  Now I wish wish to extend it further to remove .aspx from all pages.  But I am having real trouble with it, I am sure many have done this before.  How did you do it?


     

  • Re: removing .aspx extentions from url's

    Jul 10, 2008 02:19 PM|ruslany|LINK

    Can you give an example of how your URL's look like now and how you want them to look?

    Rules

  • Re: removing .aspx extentions from url's

    Jul 10, 2008 02:34 PM|Mr E|LINK

     Sure, getting http://site/another-page.aspx to http://site/another-page

     

    Not that I'm ashamed of using asp.net , I am looking at making the site more user friendly and since most users have no idea what "aspx" is, it's a good place to start.
     

  • Re: removing .aspx extentions from url's

    Jul 10, 2008 02:54 PM|ruslany|LINK

    You can have a rule similar to this one:

    <rule>
       <match url="(.*)">
       <conditions>
          <add matchType="IsFile" negate="true" />
          <add matchType="IsDirectory" negate="true" />
       </conditions>
       <action type="Rewrite" url="{R:1}.aspx"/>
    </rule>

    The conditions are necessary so that you do not rewrite url when clients request a resourse that exists on a file system. For example if client requests http://site/another-page.aspx or maybe http://site/some-image.jpg, you do not want the rule to be executed.

    Rules

  • Re: removing .aspx extentions from url's

    Jul 10, 2008 04:07 PM|Mr E|LINK

     Thank you, there were a few syntax errors but you had the right idea!
     

    It works perfectly now, here the revised code.

    <rule name="san aspx">
              <!--Removes the .aspx extension for all pages.-->
              <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>

  • Re: removing .aspx extentions from url's

    Jul 14, 2008 01:32 AM|cichanlx|LINK

    Hello.  How is it that it is not necessary to ensure that the .aspx file extension is not in the requested filename?  (For example:<add input="{REQUEST_FILENAME}" negate="true" pattern="(.*).aspx" />)  I would expect that without this condition, a request for a resource (http://site.com/abcdef.aspx) with a file name that does not exist would result in the .aspx extension being appended, thus rewriting to http://site.com/abcdef.aspx.aspx.

    Also, it would seem wise to ensure, in the rule, that the requested resource has no file extension at all to avoid having the rule fire on requests for other resources mapped to the ASP.NET ISAPI, such as Web services (.asmx files).

    Cordially,

    Lee

  • Re: removing .aspx extentions from url's

    Jul 14, 2008 01:39 AM|cichanlx|LINK

    Mr E

              <!--Removes the .aspx extension for all pages.-->

    Not to be nitpicky, but for clarity, this rewrite rule does not remove the .aspx extension; it actually adds it back.

    You remove the .aspx extension from content hyperlink URLs in the source of your Web site files (.aspx files), and this rule adds it back--invisibly to the user agent (Web browser).

    Also, I don't think there were any syntax errors.  The rule did need a name (and I think yours should be "sans aspx"); but, the {REQUEST_FILENAME} server variables you added are optional.  From the documentation:

    • <div mce_keep="true">IsFile – This match type is used to check if the input string contains a physical path of a file on a file system. If a condition input string is not specified then the URL rewrite module will use the physical path of the requested file as a default value for the condition input.</div>
    • <div mce_keep="true">IsDirectory – This match type is used to check if the input string contains a physical path of a directory on a file system. If a condition input string is not specified then the URL rewrite module will use the physical path of the requested file as a default value for the condition input.</div>

    Cordially,

    Lee

  • Re: removing .aspx extentions from url's

    Jul 14, 2008 01:03 PM|ruslany|LINK

    cichanlx

    Hello.  How is it that it is not necessary to ensure that the .aspx file extension is not in the requested filename?  (For example:<add input="{REQUEST_FILENAME}" negate="true" pattern="(.*).aspx" />)  I would expect that without this condition, a request for a resource (http://site.com/abcdef.aspx) with a file name that does not exist would result in the .aspx extension being appended, thus rewriting to http://site.com/abcdef.aspx.aspx.

    You can add this condition, but it is only necessary if your web application expects requests for aspx files that do not actually exist. Typically, in asp.net application all URL's are expected to request *.aspx files that physically exist on file system. In those cases checking for file/directory existence should be enough.

    Rules

  • Re: removing .aspx extentions from url's

    Jul 14, 2008 10:11 PM|cichanlx|LINK

    As long as Web browsers have a free-text-entry address bar, I'll be expecting the unexpected!  :]

    In addition, I'm dealing with sites that have existing "public" URLs with .aspx extensions, which I am making extensionless.  I have decided to change some of the actual file names (replacing Pascal Case with hyphens: ContactUs.aspx becomes contact-us.aspx).  Due to the lag of search results being updated and people having shortcuts and Favorites to my original .aspx names, I expect requests for .aspx files that do not exist.  Of course, I am trying to rewrite them all; but, I may miss a few until they show up in the IIS logs.

    Finally, the Default.aspx page is now known as an index.htm of sorts, so some spiders/crawlers request it without knowing for sure that it exists.

    I still think it is wise to ensure that this rule only fires for as few HTTP requests as possible.  Better than my original idea of the URL not ending with .aspx would be to ensure that the last part of the URL, after the last forward slash, contains no dot, that is no extension.

    Cordially,

    Lee C.

  • Re: removing .aspx extentions from url's

    Apr 30, 2009 03:23 AM|zohaibawan|LINK

    hello Mr E

    can u give me the complete solution how i remove the .aspx extensions. i have studied alot of articles but any site gave me no idea how to remove .aspx extension.

    i really don't know where this rule write there in web.config file can u send me the dummy solution where only one page is placed when this page run it doesn't show with extension.

    my email id is

    zohaibawan03@hotmail.com

    i m waiting ur reply

    Url Rewriting

  • Re: removing .aspx extentions from url's

    Jul 19, 2010 06:41 PM|nabeel.herzalla|LINK

    What about the Query String if I want it to be Friendly URL so the full url will be for example :

    www.contoso.com/Mail/123456 the 123456 will be the string this page (default in the mail) is accepting, I'm using ASP.net 4.0

  • Re: removing .aspx extentions from url's

    Nov 10, 2010 01:13 AM|fahadfm100@hotmail.com|LINK

    Can u please let me know that where can define rule tag.

     

    Thanks

    Rules

  • Re: removing .aspx extentions from url's

    May 18, 2011 12:45 PM|robertad|LINK

    Mr E

     Thank you, there were a few syntax errors but you had the right idea!
     

    It works perfectly now, here the revised code.

    <rule name="san aspx">
              <!--Removes the .aspx extension for all pages.-->
              <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>


     

    I have a problem. I have the same name a file and directory so the rule doesn't work. 

  • Re: removing .aspx extentions from url's

    Nov 23, 2011 08:56 AM|tinomclaren|LINK

    Please also remember that once this rule is applied you will need to update your links on your pages from 'whatever.aspx' to 'whatever' Basic stuff but had me on the left foot for a tad.
    Never argue with an idiot strangers cant tell the difference....
  • Re: removing .aspx extentions from url's

    Dec 24, 2012 01:07 AM|priyankafanse|LINK

    Hello,


    Can you please tell where to put this whole section of code in web.config. Because i have put in <system.webServer> tag. But its not working. Also i tried by replacing the links for redirection (say for e.g dashboard.aspx to dashboard) but then also no success Frown.

    Can you help me please to do this.

    Thanks in Advance...

  • Re: removing .aspx extentions from url's

    Dec 27, 2012 11:30 AM|murtaza_t|LINK

    Hello,

    Please ensure that you are using IIS version 7 or above as rewrite module was not available in earlier versions. Also please ensure that IIS rewrite module is installed from Control Panel >> Programs. If both these statements are true then here is an example of web.config:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <defaultDocument>
          <!-- Set the default document -->
          <files>
          <!-- Add / remove files -->
          </files>
        </defaultDocument>
    <rewrite>
    <rules>
            <rule name="san aspx">
              <!--Removes the .aspx extension for all pages.-->
              <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>
    </configuration>


    Please Mark the post as Answer if it has helped you.
    Regards,
    Murtaza
  • Re: removing .aspx extentions from url's

    Sep 01, 2016 08:49 AM|Umesh Maurya|LINK

    Hi nabeel.herzalla, My problem is similar to you.

    Have you got the solution for above. As the code for removing .aspx also removes the complete query string or path after .aspx

    I want www.travelxp.com/flightresults.aspx/123456/bom/del/22-05-2016

    should rewrites to

    www.travelxp.com/flightresults/123456/bom/del/22-05-2016

  • Re: removing .aspx extentions from url's

    Sep 01, 2016 08:55 AM|Umesh Maurya|LINK

    hi ruslany, My url is www.travelxp.com/flightresults.aspx/123456/bom/del/22-05-2016 and i want to rewrite it to

    www.travelxp.com/flightresults/123456/bom/del/22-05-2016

    but the code below is resulting as :  www.travelxp.com/flightresults

    <rule name="RemoveASPX" enabled="true" stopProcessing="true">
              <match url="(.*)\.aspx" />        
              <action type="Redirect" url="{R:1}" />
            </rule>
          
            <rule name="AddASPX" enabled="true">
                <match url="(.*)" negate="false" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    <add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
                
                </conditions>
                <action type="Rewrite" url="{R:0}.aspx" appendQueryString="false" />
            </rule>

    Kindly help me. thanks