locked
removing .aspx extentions from url's RRS feed

  • Question

  • User-287276650 posted

     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?


     

    Thursday, July 10, 2008 2:11 PM

All replies

  • User-1637866776 posted

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

    Thursday, July 10, 2008 2:19 PM
  • User-287276650 posted

     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.
     

    Thursday, July 10, 2008 2:34 PM
  • User-1637866776 posted

    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.

    Thursday, July 10, 2008 2:54 PM
  • User-287276650 posted

     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>

    Thursday, July 10, 2008 4:07 PM
  • User-115762274 posted

    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

    Monday, July 14, 2008 1:32 AM
  • User-115762274 posted

              <!--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:

    • 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.
    • 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.

    Cordially,

    Lee

    Monday, July 14, 2008 1:39 AM
  • User-1637866776 posted

    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.

    Monday, July 14, 2008 1:03 PM
  • User-115762274 posted

    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.

    Monday, July 14, 2008 10:11 PM
  • User-1612189579 posted

    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

    Thursday, April 30, 2009 3:23 AM
  • User-556593088 posted

    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

    Monday, July 19, 2010 6:41 PM
  • User-1455189015 posted

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

     

    Thanks

    Wednesday, November 10, 2010 1:13 AM
  • User-631298286 posted

     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. 

    Wednesday, May 18, 2011 12:45 PM
  • User1995728413 posted
    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.
    Wednesday, November 23, 2011 8:56 AM
  • User524166766 posted

    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...

    Monday, December 24, 2012 1:07 AM
  • User-597491400 posted

    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>
    


    Thursday, December 27, 2012 11:30 AM
  • User461520192 posted

    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

    Thursday, September 1, 2016 8:49 AM
  • User461520192 posted

    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

    Thursday, September 1, 2016 8:55 AM