Sitecore URL Rewriter module

10 maart 2014 om 00:00 by Ruud van Falier - Post a comment

One of the most requested features for our Sitecore implementations is URL rewriting;
the ability to permanently redirect a requested URL to a different URL.
The most obvious situation in which this is used is when the URL of your pages has changed or when you move your site to a new domain.

We have built this functionality some time ago and recently decided to release it as a stand-alone module so that everyone can use it in their own Sitecore solution.
This blog post describes what the module does and how to use it.

The module was built with the following goals in mind:

  • It must be able to rewrite relative or absolute URL’s.
  • It must be able to rewrite hostnames, while keeping the requested path intact.
  • Content editors must be able to manage the rewrite rules (keep it simple).

The resulting module works by defining URL or hostname rewrite rules as Sitecore items that are managed in the Content Editor.
It can be installed using one Sitecore Installation Package and requires no further configuration.

Rewrite rule items

The module adds three new templates to Sitecore:

  • Rewrite rules folder
  • URL rewrite rule
  • Hostname rewrite rule


Rewrite rules folder
This template is used to define an item in Sitecore which will be the root folder for all rewrite rules.
By default, the module will create one rewrite rules folder: /sitecore/System/Modules/URL Rewriter rules.

URL rewrite rule
If you need to rewrite a relative URL (e.g. /my-folder/my-page.html) or absolute URL (e.g. http://mydomain.com/my-folder/my-page.html) you create a URL rewrite rule.

Hostname rewrite rule
If you need to rewrite the hostname of a request, while keeping the rest of the URL intact, you create a hostname rewrite rule.

A rule item consist of just two fields: Source URL/Hostname and Target URL/Hostname.
All the rules are cached during the first request after Sitecore has been started.

Defining URL rewrite rules

When you create a URL rewrite rule, you define a source URL (the request URL that you want to rewrite) and a target URL (the URL where the request should be redirected to).

For both fields, you must specify at least a relative URL, so / would be the minimum valid value.
If you specify a hostname as well, you must specify an absolute URL, including the protocol prefix (e.g. http://www.mydomain.com/my-page).
In the case of relative URL values, the hostname from the current request will be used during the rewrite.

The query string of your request will be kept intact during the rewrite, unless you explicitly define one in the target URL.

Examples:

Source URL = http://www.source.com/my-old-page.html
Target URL = http://www.target.com/my-new-page.aspx

In this case, a request to: http://www.source.com/my-old-page.html
will be redirected to: http://www.target.com/my-new-page.aspx

Because the target URL does not contain query string, the query string from the initial request is kept intact,
so a request to: http://www.source.com/my-old-page.html?myquery=value
will be redirected to: http://www.target.com/my-new-page.aspx?myquery=value

If a query string would have been defined on the target URL, it will overwrite any existing query string:

Source URL = http://www.source.com/my-old-page.html
Target URL = http://www.target.com/my-new-page.aspx?my-explicit=querystring

In that case, a request to: http://www.source.com/my-old-page.html?myquery=value
is redirected to: http://www.target.com/my-new-page.aspx?my-explicit=querystring

Defining hostname rewrite rules

When you create a hostname rewrite rule, you define a source hostname (the hostname for which the requests need to be rewritten) and a target hostname (the hostname the request need to be redirected to).
Hostname rewrite rules rewrite the hostname of a request URL, but keep the rest of the URL intact.
Both fields accept a hostname or IP-address as a value.

Example:

Source hostname = www.sourcedomain.com
Target hostname = www.mynewdomain.com

In that case, a request to: http://www.sourcedomain.com/my-path/my-document.html?my=querystring
is redirected to: http://www.mynewdomain.com/my-path/my-document.html?my=querystring

Remove trailing slashes

It’s considered an SEO best practice to remove trailing slashes from request URL’s.
Although we have already implemented this in our SEO-friendly URL module, we have added it as an optional feature to this module as well.

The module will rewrite all request URL’s that contain a trailing slash to the same URL without trailing slash.
This behavior can be disabled in the configuration and is enabled by default.

Rewrite rule cache

As mentioned before, the rewrite rules are cached during the first request after Sitecore has been (re)started.
If you make changes to rewrite rules, you need to clear the cache using the Clear cache button in the Publish ribbon, otherwise the changes will not be applied.



Note that you need to have publishing rights in order to see this button.

If you are using a multi-instance environment (i.e. you have separate Content Management and Content Delivery instances), the cache is cleared on all the instances (assuming that EventQueues are enabled).

Logging

The module writes an entry to the Sitecore log every time a request is redirected by the module.
This behavior can be disabled in the configuration file.

Here is an example of the output:

9584 14:48:07 INFO  ParTech.Modules.UrlRewriter: Cached 3 URL rewrite rules and 2 hostname rewrite rules.
9584 14:50:07 INFO ParTech.Modules.UrlRewriter: Removed trailing slash from 'http://www.new-website.com/blog/'.
9584 14:50:22 INFO ParTech.Modules.UrlRewriter: URL rewrite rule '{2B0EC9E4-E64C-4773-82C3-AC702A5F3002}' caused the requested URL 'http://www.new-website.com/blog/removed-post.aspx' to be rewritten to 'http://www.new-website.com/blog/replacement-post.aspx'
9584 14:50:29 INFO ParTech.Modules.UrlRewriter: Hostname rewrite rule '{CDA1B192-E220-4445-8687-C75DD3B6706D}' caused the requested URL 'http://www.old-domain.com/news/overview.aspx' to be rewritten to 'http://www.new-website.com/news/overview.aspx'

Information about rules being cached or error messages from the module will always be logged, even if logging rewrites is disabled in the configuration.

Things to keep in mind

  • URL rewriting is only applied to GET requests.
  • Requests to parts of the Sitecore Client are ignored and you can ignore other sites as well by adding them to the ignore list in the module configuration file.
  • If you like the URL Rewriter rules folder to be in a different location in the Sitecore tree, your best options is to move it. If you decide to delete it and create a new one, you need to update the RulesFolderId setting in the module configuration file.

The module is tested with the latest revisions of Sitecore 6.5 up to 7.1
It should work just fine with older versions such as 6.3 / 6.4 and all other revisions as well.

Future features

We are planning to add several more features in the nearby future:

  • Specify multiple sources in one rule.
  • Clear cache automatically when rule items are published.
  • Use regular expressions in source and target values.
  • Support different redirect status codes (302, 307, 308).

If you can’t wait for us to implement them, feel free to fork the GitHub repository and start contributing to this project.

Download the module

The module is now available on GitHub: https://github.com/ParTech/Url-Rewriter.
You can download the Sitecore Installation Package (updated version) and install it in your own environment using the Installation Wizard in the Sitecore Client.

It will also become available on the Sitecore Marketplace shortly after publication of this blog post.

As always, your feedback is much appreciated, so let us know if you have any questions, comments or suggestions!

Update

We have released a new update for this module that fixes an issue that a few users experiences.
Read more about this update.

Nieuwste