Belangrijke mededeling over deze pagina:

Deze pagina is niet beschikbaar in het Nederlands en daarom wordt de Engelse versie van het artikel getoond. Dit komt vooral voor bij technische artikelen.

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.

15 Reacties

  • Door Ruud van Falier op 4/8/2015 om 4:05 PM

    It supports every version from 6.3 and up (not tested with older, but should be no problem either).

  • Door Prabhakaran op 4/8/2015 om 3:35 PM

    Does this module support sitecore 7.5 ?

  • Door Ruud van Falier op 3/3/2015 om 8:18 AM

    Hi Golois,

    Yes it does.
    It supports every version from 6.3 and up (not tested with older, but should be no problem either).

    Cheers,
    Ruud

  • Door Golois op 3/2/2015 om 6:29 PM

    Does this module support sitecore 7.2?

    Thanks

  • Door Ruud van Falier op 1/21/2015 om 4:01 PM

    Hi Pravin,

    You can, but the URL will always act as a redirect.

    Ruud

  • Door Pravin Giradkar op 1/21/2015 om 2:53 PM

    Can we use this module for creating Vanity URLs.

  • Door Ola Gjerde op 10/13/2014 om 10:28 AM

    Hi,
    Have some problems with the module. Same problem that other people have. We love this module, but site it going offline when this error occurs.

    Do you have a fix for this ?

    Here is my error:

    *Removed stacktrace*

  • Door Ruud van Falier op 8/29/2014 om 10:25 AM

    Hi priyanka,

    This seems to be exactly the same issue as Vinod is experiencing. It's currently under investigation and we hope to fix it soon.

    Thanks,
    Ruud

  • Door priyanka op 8/29/2014 om 9:51 AM

    Thanks Van for the information, I am using multi server architecture with content authoring and content delivery servers, however it has been observed recently that sometimes when we have a scheduled app pool recycle the application fails with object reference error(same as highlighted by caller Vinod) at one of the content delivery servers. Also the partech module shows more number of rewrite rules cached in the list(in multiple specially). Does this mean that static object(S) are being shared at a common location for the CD servers? Is it some sort of default action for the module in multi server architecture? I do see in logs for Authoring server it is working fine.

  • Door Ruud van Falier op 8/28/2014 om 1:42 PM

    Hi Priyanka,

    The rules are stored in a List<> and cached using a static object that is shared across the application.
    So it does not use the Sitecore cache, but its own caching.
    The cache is automatically refreshed when changes are made to rules and the module also comes with a button in the Content Editor for refreshing the cache manually.

    Cheers,
    Ruud

  • Door priyanka op 8/28/2014 om 1:08 PM

    Hi Van,

    Just wanted to know, does this module store rewrite rules in some sport of datastructure(a list type) and then store that structure in cache? Also could you please suggest in which sitecore cache does it store the cache?

  • Door Ruud van Falier op 8/28/2014 om 8:31 AM

    Hi Vinod,

    I'm sending you an email about this. Thanks for reporting it.

    Ruud

  • Door Vinod Singh op 8/28/2014 om 7:59 AM

    Hi All,

    We are using this module since long time in production environment. But sometimes this module is causing the problem in live site and throwing the object null reference error. Please see the following errors have been captured in live site.

    *Removed stacktrace*

    Please let me know why this errors are happening in live environment?

    Regards,
    Vinod

  • Door Ruud van Falier op 8/26/2014 om 9:11 AM

    Hi Budi,

    It certainly does!

    Cheers,
    Ruud

  • Door Budi op 8/5/2014 om 8:40 PM

    Does this module support sitecore 6.6?

    Thanks,
    Budi

Plaats een reactie