More in this section

Forums / Developing with Sitefinity / Is this urlrewrite possible?

Is this urlrewrite possible?

14 posts, 0 answered
  1. jcrichto
    jcrichto avatar
    35 posts
    Registered:
    14 Aug 2012
    22 Jan 2009
    Link to this post
    Wanting to do urlrewrite of non-extension urls.  Can the urlrewrite work like this?

    <urlrewrites>
            <rule>
                <url>test</url>
                <rewrite>test.aspx</rewrite>
            </rule>
    </urlrewrites>

    Basically, we need extensionless page urls.  We do this all the time with mod-rewrite and apache. 

    Thanks.

    Jeremy
  2. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    23 Jan 2009
    Link to this post
    Hi Jeremy,

    I think Telerik's answer will be to use the method outlined in the user guide on Extensionless URLs (http://www.sitefinity.com/documents/UserManual_3_5.pdf  on page 285).

    You can also use IIS wildcard mappings (described in the following section of the user manual), but there is a performance hit, because then the ASP.NET ISAPI will process every single request to the site, including requests for static resources such as images. 

    The problem isn't so much with Sitefinity as with IIS.  Basically if IIS sees a request for a URL without an extension, it automatically returns an HTTP 404.  The approach in the User Guide says to create a page called sf404.aspx, and configure IIS to use that page on a 404.  Then, presumably, you can rewrite the URL, because it's an ASP.NET page, and now Sitefinity can get hold of it.

    The Wildcard mapping directs any request to ISAPI for processing.

    But I'd been wondering if there might be a better way, outside of Sitefinity, and found this article:

    http://www.kowitz.net/archive/2006/09/15/url-rewriting-using-ihttpmodule-vs-isapi-rewrite.aspx

    He's advocating a hybrid approach; write your own IHttpHandler ISAPI filter (which hides the .aspx nicely) and an IHttpModule to handle all the virtual paths like ~/ and then to rewrite urls back to be extensionless.  Since Sitefinity seems to have URL rewriting down cold, as long as you're processing it within the ASP.NET stack, writing the ISAPI filter on the IIS side of things seems like a good idea to try.  I haven't given this a shot, though, so if you try it, I'd like to hear about your results.

    Regards,
    Mike Sharp
  3. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    23 Jan 2009
    Link to this post
    Hello,

    Thanks Mike for your answer! We updated your Telerik account for helpful and well structured reply!

    Regards,
    Georgi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  4. jcrichto
    jcrichto avatar
    35 posts
    Registered:
    14 Aug 2012
    23 Jan 2009
    Link to this post
    Yes, thanks Mike.  That was a very good post!  I will look into this.
  5. jcrichto
    jcrichto avatar
    35 posts
    Registered:
    14 Aug 2012
    23 Jan 2009
    Link to this post
    Just to let you know, I seem to have respectable rewrites working with the lite version of this, and nothing else.

    http://www.helicontech.com/download-isapi_rewrite3.htm

    Very cool product, lets you test and apply your rewrites globally using normal mod-rewrite syntax.  I only had to make on rewrite statement, and so far it is doing everything I would expect it to.  I may run into some issues I haven't tested yet...like the ~, but we will see.

    My httpd.conf looks like this.

    RewriteEngine On
    RewriteRule ^(.+[^\.(aspx|gif|jpe?g|png|css|js)^/])$ $1.aspx [L]

  6. jcrichto
    jcrichto avatar
    35 posts
    Registered:
    14 Aug 2012
    25 Jan 2009
    Link to this post
    .. forgot about allowing querystrings.  Final config is .

    RewriteRule ^(.+[^\.(aspx|gif|jpe?g|png|css|js)][^?.*][^/])$ $1.aspx



    Also, I should mention that I am running in a domain (www.stie.com) not in a subfolder, and I always use root relative paths for everything.  EX: "/images/test.png", "/products/test", etc.
  7. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    26 Jan 2009
    Link to this post
    Hello Jeremy,

    This is a cool thing indeed. Thank you that you share it with us!

    Kind regards,
    Georgi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  8. jcrichto
    jcrichto avatar
    35 posts
    Registered:
    14 Aug 2012
    27 Jan 2009
    Link to this post
    Turns out the above syntax wasn't exactly working.  Went with conditions instead.

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !\.(aspx|css|js|jpe?g|gif|png|swf|ashx)$ [NC]
    RewriteCond %{REQUEST_URI} !\?.*$
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule ^(.+)$ $1.aspx


  9. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    04 Feb 2009
    Link to this post
    Thanks for the tip--I owe you a brew!  ;^)

    You mentioned you were using it in the root of a domain, and I was wondering how to get it working in the dev environment where the site is in a subfolder.  All the links to CSS and such are broken in that environment...for some reason Sitefinity is requesting the images and CSS without the project folder prefixed to the request.

    After a bit of thought, this occurred to me (I name my projects the same as the producton domain):

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !\.(aspx|css|js|jpe?g|gif|png|swf|ashx)$ [NC]
    RewriteCond %{REQUEST_URI} !\?.*$
    RewriteCond %{REQUEST_URI} !/$
    RewriteCond %{REQUEST_URI} (projectsite.com) [NC]
    RewriteRule (.*)(projectsite.com/)(.+) $1$2sites/$3.aspx [NC]

    RewriteCond %{REQUEST_URI} !(projectsite.com) [NC]
    RewriteCond %{HTTP_REFERER} (projectsite.com) [NC]
    RewriteRule ^(/.*)$ /projectsite.com$1 [NC]

    In my case, I wanted to rewrite a request for:

    /extensionlesspage

    to

    /sites/extensionlesspage.aspx

    But I didn't want it to affect the othe projects in the dev environment, so I couldn't just rewrite all requests for resources that begin with "/".  So far this seems to be working.  And in production, I shouldn't need the second group of rewrite conditions and their rule.

    Again, thanks for the tip.  This is WAY easier than the alternative!  ;^)


    Regards,
    Mike Sharp
  10. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    06 Feb 2009
    Link to this post
    Noticed a couple bugs in the rewrite conditions...the extension list should include AXD resource files (maybe TMB if you're using them), and in my particular case, I need to prevent rewrites like:

    http://www.somesite.com/sitefinity/admin

    So my latest set of rules rewrite a request for:

    /extensionlesspage

    to

    /sites/extensionlesspage.aspx

    and hopefully ignore anything else!  ;^)

    # Rewrites extensionlessURLs
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !\.(aspx|css|js|jpe?g|gif|png|swf|ashx|axd)$ [NC]
    RewriteCond %{REQUEST_URI} !\?.*$
    RewriteCond %{REQUEST_URI} !/$
    RewriteCond %{REQUEST_URI} !somesite.com/.+/.+$ [NC]
    RewriteCond %{REQUEST_URI} (somesite.com) [NC]
    RewriteRule (.*)(somesite.com/)(.+) $1$2hubs/$3.aspx [NC]

    # For urls from the site root, like /foo.aspx, so that it works in dev
    RewriteCond %{REQUEST_URI} !(somesite.com) [NC]
    RewriteCond %{HTTP_REFERER} (somesite.com) [NC]
    RewriteRule ^(/.*)$ /somesite.com$1 [NC]


    Man, I love this tool!

    Mike

  11. jcrichto
    jcrichto avatar
    35 posts
    Registered:
    14 Aug 2012
    06 Feb 2009
    Link to this post
    Thanks Mike.

    I meant to post my revisions including the axd.

    BTW, another thing to watch out for is when somebody accidentally puts a  space after the extension. 
    Mine ended up looking like this:

    RewriteCond %{REQUEST_URI} !\.(aspx|css|js|jpe?g|gif|png|swf|ashx|axd)\s*$ [NC]

    Great solution for the subfolder on dev!

    - Jeremy
  12. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    06 Feb 2009
    Link to this post
    Oh yeah, nice catch.  That's very likely on my site, as people will often be pasting URLs they get in an email from someone.

    Thanks,
    Mike
  13. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    07 Feb 2009
    Link to this post

    I think I've run into a showstopper bug using this rewrite approach.  The problem exists when trying to do a postback on a rewritten page. 

    If I rewrite /testing to /sites/testing.aspx, the form wants to postback to /testing.aspx.  In .NET 2.0, you can't modify the action attribute, though that's permissible in 3.5. 

    If I try to modify the control that's posting back by changing it's PostBackUrl pproperty, then ASP.NET doesn't treat it as a postback, but rather a form post, so my command_click code never gets executed.

    I browsed the related forum posts on Helicon's site, but didn't find anything that looked like a solution (most of the solutions wouldn't work with AJAX).  However, I did find a blog by Scott Guthrie that talks about this.  He covers the scenario near the  end, under "Handling ASP.NET PostBacks with URL Rewriting"

    The trick is that he uses a control adaptor in the App_Browsers to overwrite the form action on the client.  He provides a sample that uses a different URL Rewriter, that's implemented as an HttpModule (which would be better for me, since I'm not sure that Mosso is going to let me install an ISAPI filter...

    The blog is at:

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

    His sample can be downloaded at:

    http://www.scottgu.com/blogposts/urlrewrite/UrlRewrite_HttpModule1.zip

    I haven't tried it for my particular scenario yet, but it's looking good. The rewrite rules go in the web.config, and the rewriter is just a DLL in the application bin.  This would be great for my scenario...but I'm not sure if it's going to work for extensionless URLs in .NET 2.0...

    Anyway, I'll let everyone know how this turns out.

    Mike

     

     

  14. Dino
    Dino avatar
    24 posts
    Registered:
    20 Dec 2008
    17 Feb 2009
    Link to this post
    Thank you all who posted for your insight to url rewriting.  It has helped quite a bit.

    Could this possibly be a fix to avoid duplicate access to the same content page?  Search engines don't like if you can access /testpage.aspx as well as /testpage

    #Redirect extension to avoid duplicate content
    #Ignore redirect on .aspx pages in Sitefinity
    RewriteCond %{REQUEST_URI} !site.com/.+/.+$ [NC]
    RewriteRule ^([^?]+)\.aspx$ $1 [NC,R=301,L]

    #Internally add extensions to request
    RewriteCond %{REQUEST_URI} !\.(aspx|css|js|jpe?g|gif|png|swf|ashx|axd)$ [NC]
    RewriteCond %{REQUEST_URI} !\?.*$
    RewriteCond %{REQUEST_URI} !/$ 
    RewriteCond %{REQUEST_URI} !site.com/.+/.+$ [NC]
    RewriteCond %{REQUEST_URI} (site.com) [NC]
    RewriteRule (.*)(site.com/)(.+) $1$2/$3.aspx [NC]


Register for webinar
14 posts, 0 answered