« Previous Next »

Answered Thread: Rewriting Extensionless URLs

Last post 11-02-2009 4:48 PM by devgineer. 9 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (10 items)

Sort Posts:

  • 09-30-2009, 5:33 AM

    Rewriting Extensionless URLs

    Hi I have the following two rules setup to redirect to an extensionless url and to rewrite the extensionless url back to the aspx page.

    <rule name="Redirect aspx to extensionless" stopProcessing="true">
              <match url="(.*)\.aspx" />
              <action type="Redirect" url="{R:1}" redirectType="Permanent" />
    <rule name="rewrtie the aspx extension">
              <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" redirectType="Permanent"/>
    </rule>

    The rules are actually working but now my link buttons and ajax tab controls don't work. Any ideas on how I can fix these so that my .net controls are working? I think it has something to do with the .axd files being rewritten to a .aspx extension but I'm not 100% sure.

     Thanks,

    Joshua

  • 09-30-2009, 9:07 AM In reply to

    Re: Rewriting Extensionless URLs

    Well, you're matching every extension and changing it, so you're correct in your assumption about .axd files.  Not sure there's any easy fix to this without testing for wanted file extensions.

    Jeff

    Look for Wrox's new book Professional IIS 7 in your local bookstore, or order now at Amazon.com
  • 10-01-2009, 6:56 PM In reply to

    • ruslany
    • Top 25 Contributor
    • Joined on 07-01-2007, 7:38 PM
    • Redmond, WA
    • Posts 670

    Answered Re: Rewriting Extensionless URLs

    You need to add an extra condition to the second rule to avoid rewriting of request to *.axd files:

        <add input="{URL}" negate="true" pattern="\.axd$" />

    For more information refer here: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/, tip #10.

    http://ruslany.net
  • 10-01-2009, 10:52 PM In reply to

    Answered Re: Rewriting Extensionless URLs

    ruslany:

    You need to add an extra condition to the second rule to avoid rewriting of request to *.axd files:

        <add input="{URL}" negate="true" pattern="\.axd$" />

    For more information refer here: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/, tip #10.

    That is actually the first page I came accross when I decided to learn the IIS url rewrite syntax. Your suggestion worked perfectly. Here is my complete web.config section for URL rewrite. Any suggestions for optimizing it?

    <rewrite>
          <rules>
    <rule name="Redirect aspx to extensionless" stopProcessing="true">
              <match url="(.*)\.aspx" />
              <action type="Redirect" url="{R:1}" redirectType="Permanent" />
    </rule>
     <rule name="Redirect to WWW" stopProcessing="true">
              <match url="(.*)" />
              <conditions>
                <add input="{HTTP_HOST}" negate="true" pattern="^www\.domain\.com$" />
              </conditions>
              <action type="Redirect" url="http://www.domain.com/{R:1}" redirectType="Permanent" />
            </rule> 

    <rule name="redirect default extensionless document" stopProcessing="true">
     <match url="(.*)default" />
              <action type="Redirect" url="{R:1}" redirectType="Permanent" />
            </rule>

    <rule name="rewrtie the aspx extension">
              <match url="(.*)" />
              <conditions logicalGrouping="MatchAll">
    <add input="{URL}" negate="true" pattern="\.axd$" />
              <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
              <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
              </conditions>
              <action type="Rewrite" url="{R:1}.aspx" redirectType="Permanent"/>
    </rule>

    </rules>
    </rewrite>

    One other question what other rule can I add to turn a url like this
    www.domain.com/search?subject=keyword
    to
    www.domain.com/search/subject/keyword or
    www.domain.com/subject/keyword

    thanks again!

  • 10-07-2009, 4:03 AM In reply to

    Re: Rewriting Extensionless URLs

    Does anybody know why the redirects break the session state? If I remove all the redirect rules I can use session state if I go directly to an extensionless url. So if I type in www.mydomain.com/directory/file the session state works fine and the url gets correctly rewritten. If I put in the redirect rules and go to www.mydomain.com/directory/file.aspx it get's redirected to www.mydomain.com/directory/file and rewritten fine but my session does not work. I've already added the  runAllManagedModulesForAllRequests="true" attritbute to the modules element web.config and it doesn't make a difference. Any pointers would be greatly appreciated.

  • 10-07-2009, 4:25 PM In reply to

    • ruslany
    • Top 25 Contributor
    • Joined on 07-01-2007, 7:38 PM
    • Redmond, WA
    • Posts 670

    Re: Rewriting Extensionless URLs

    Regarding the existing rules - I would recommend to use anchored regex patterns, e.g. (.*)\.aspx$ and (.*)default$

    As for the questions: this article may help: http://learn.iis.net/page.aspx/497/user-friendly-url---rule-template/ 

     

    http://ruslany.net
  • 10-07-2009, 4:25 PM In reply to

    • ruslany
    • Top 25 Contributor
    • Joined on 07-01-2007, 7:38 PM
    • Redmond, WA
    • Posts 670

    Re: Rewriting Extensionless URLs

    devgineer:

    Does anybody know why the redirects break the session state?

    Is this a cookie based session? May be the cookie gets lost during redirection?

    http://ruslany.net
  • 10-08-2009, 3:33 AM In reply to

    Re: Rewriting Extensionless URLs

    Thanks Ruslan,

    That link was exactly what I needed for the query string problem. As for the session, the anchored regex pattern didn't help. This is what my redirect looks like now.
    <rule name="Redirect aspx to extensionless" stopProcessing="true">
              <match url="(.*)\.aspx$" />
              <action type="Redirect" url="{R:1}" redirectType="Permanent" />
    </rule>

    I tried removing the stopProcessing attribute, and also removing and adding session back in the modules collection via this recommendation http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=357248
    Here is the post that lead me to that bug on Microsoft Connect
    http://forums.iis.net/t/1151614.aspx

    I've tried putting my 'rewrite' collection before and after the 'modules' collection. None of these help with the session. Here is my modlues collection.
        <modules runAllManagedModulesForAllRequests="true">
          <remove name="ScriptModule"/>
          <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <remove name="Session" />
    <add name="Session" type="System.Web.SessionState.SessionStateModule" preCondition="" />
        </modules>

    As for cookies, I am not using cookieless sessions. Right now this is on a staging site on a subdomain and the www redirect rule is not in web.config so the cookie's shouldn't be getting lost through the redirection. I am completely lost as to why the session isn't working based on other posts/workarounds I have worked and tried.

  • 10-08-2009, 3:06 PM In reply to

    • ruslany
    • Top 25 Contributor
    • Joined on 07-01-2007, 7:38 PM
    • Redmond, WA
    • Posts 670

    Re: Rewriting Extensionless URLs

    The anchored regex suggestion was not related to the session problem.

    The only thing I can think of right now which may help with investigation is enable Fiddler and see what are the headers that are being used during redirection.

    http://ruslany.net
  • 11-02-2009, 4:48 PM In reply to

    Re: Rewriting Extensionless URLs

    Hi Ruslany,

    Didn't mean to drop this thread, I really appreciate your quick replies, I had just put my IIS projects with the session/rewrite issue on the backburner. I'm going to play around with Fiddler and Firebug to see what I could find. Do you have any technical information on how rewriting works, not any info on writing the rules themselves.

    Thanks,

    Joshua

Page 1 of 1 (10 items)
Microsoft Communities