Skip to main content

Cloud

Implement Friendly URLs for SharePoint Blog Sites

By default, SharePoint uses URLs like the following for the page that displays a post on a blog site:
http://blogs.mycompany.com/blogs/blogger_name/lists/posts/post.aspx?ID=1
With a little code in an HTTP Module, we can support a much more user friendly URL format for displaying posts. In this example, I will show you how to create a module that implements URL rewriting and response filtering to support URLs that look like this instead:
http://blogs.mycompany.com/blogs/blogger_name/1/blog_post_title
Not only is this format more friendly to users but it can also increase your search engine page ranking by including keywords from the post title in the URL.
To achieve this, we will create a module that accomplishes two things: 1) filter all responses in the blogs site to modify any hyperlinks to the Post.aspx to use the user-friendly URL instead; 2) rewrite requests for the user-friendly URLs into a format that ASP.NET can use to pass the request off to the Post.aspx page.
Below is the code of the module. All the module itself does is set up the URL patterns that need to be processed by the module and then hook into the BeginRequest event to determine whether or not the current request matches one of the configured routes. If it does, the we execute the HandleRoute method on the matched route.
All of the work of matching the current request to a route is done by the System.Web.Routing.RouteCollection class when we call GetRouteData. All we have to do is set up the routes by specifying a matching pattern and providing an implementation of IRouteHandler. The real work of the module is done within the IRouteHandler implementations that we delegate to when we call HandleRoute.

Now we will examine how to implement the response filtering and URL rewriting. For the first, I’ve created a class called BaseHandler that implements IRouteHandler. Note that we’ve implemented IRouteHandler so that we can use the RouteData class as a convenient storage place for our route handler. We do not actually need the functionality provided by the interface.
Inside this HandleRoute method of this class, we modify the response to ensure that it is buffered and then set up a filter to handle making the changes to the URLs in any hyperlinks on the page. This filter will parse the response and modify it as needed before ASP.NET sends it to the browser.

This parsing and fixing is done in the UrlFixupFilter class. This class is an implementation of Stream that simply delegates to the Stream that was passed into the constructor for all methods except for Write. In Write, we make our modifications to the buffered content before delegating to the Write method of the underlying stream.
The code to make the modifications is found in the replaceUrls method. This method utilizes a regular expression to find and replace the href attribute of any hyperlinks to the Post.aspx page. After this code executes, these hyperlinks will point to the user-friendly URL instead.

One additional note on this code, I added a check at line 133 to prevent modification to the Permalink hyperlink that SharePoint renders for blog posts.
Last, we’ll take a look at the route handler that handlers the requests to the user-friendly URLs. This handler simply rewrites the URL to Post.aspx and appends the post ID to the query string using the value passed-in via the RouteData object.

By deploying this module to our SharePoint application, we can utilize URLs that are much more user-friendly, and search-engine-friendly as well, while still leveraging all of SharePoint’s out of the box blog functionality.
In this post, we examined two very powerful mechanisms for enhancing SharePoint or ASP.NET applications. First, URL rewriting allows us to support friendly URLs within our applications. These URLs need not point to the .aspx file that will ultimately handle the request. Instead our module provides a mapping layer that routes requests to the .aspx file without the knowledge of the user. Second, we utilized post-processing on the response to filter out the ugly old URLs in hyperlinks and replace them with our clean, friendly URLs before the response is sent to the browser. In doing this, we leveraged the System.Web.Routing namespace which was introduced to ASP.NET for use in the MVC Framework but provides us with lots of useful capabilities in our non-MVC applications as well.
Some potential enhancements that could be explored include 1) making the module more configurable so that it is not coupled tightly to the URL structure hardcoded into the Route definitions; 2) adding routes to provide friendly URLs for the Category.aspx page; and 3) updating the filter class so that it does its URL modification based on data defined in the RouteCollection instead of using hardcoded strings.

Tags

Thoughts on “Implement Friendly URLs for SharePoint Blog Sites”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

PointBridge Blogs

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram