How to: 302 Tuckey URL rewrites when implementing a new content management system (CMS).

When using content management systems -- CMS Tuckey URL rewrites can help you maximize Search Engine Optimization -- SEO performance, particularly when migrating your website to a new environment, fixing dead or broken links, or optimizing your website.  It is important to make sure you know when to use 301 or 302 redirects and when to use aliases.  For further information on 301, 302, and aliases, please visit my previous blog post on that subject.  This article focuses on how to leverage Tucky URL rewrites and 302 redirects as they are the most beneficial to Search Engine Optimization -- SEO performance.

In this blog post, we are going to take a technical dive into Tuckey URL Rewrite filter and turn these ideas into practice.  A very important part of URL rewrites and redirects is understanding regular expressions. Regular expressions give us the ability to create search patterns in order to match a given URL.  Getting comfortable with regular expressions is the first step to take when attempting to tackle URL rewrites.  After familiarizing yourself with basic RegEx patterns, we are ready to move on to our first example.

 

Examples

Let’s say we have a page on our site that was removed but we know that the link still exists out on various other websites.  This would be a good opportunity to prevent traffic from landing on a 404 by directing them to a new page. The tuckey rewrite rule for this might look like this:

                <rule>

                                <from>^/old-page$</from>

                                <to last=”true” type=”redirect”>/new-page</to>

                </rule>

This will direct all traffic that lands on www.mydomain.com/old-page to www.mydomain.com/new-page and prevent them from seeing the dreaded 404.  Knowing that this page is one that has been moved permanently, we can modify the rule to ensure that a 301 status code is returned for a better SEO ranking.

                <rule>

                                <from>^/old-page$</from>

                                <to last=”true” type="permanent-redirect">/new-page</to>

                </rule>

If you were to visit your site you would now see that the /old-page page is redirected correctly to /new-page.  Pages will also sometimes carry a trailing / on their URL’s.  In the redirect rule we implemented above, the case of /old-page/ would not have matched.  In order to account for the trailing slash, we don’t need to write a whole new rule, we can simply add an optional modifier to our <from> clause:

                                <from>^/old-page(/)?$</from>

                                <to last=”true” type="permanent-redirect">/new-page</to>

 

 

While sometimes we may need to redirect a single page, often times we may need a way redirect groups of pages.  Let’s say we have a group of pages on our site: /contact/john,  /contact/mary, and /contact/sue.  Recently a change was made and they were all combined into a single contact-us page at /contact-us.  We might think to make three rules, one for each of the contact pages.  But this is where our RegEx knowledge will help combine rules and keep our rules clean:

                <rule>

                                <from>^/contact/(john|mary|sue)$</from>

                                <to last=”true”  type="permanent-redirect">/contact-us</to>

                </rule>

In some cases you might not just have a couple pages, you may have hundreds.  In this case the above solution would be come very tedious and time-consuming.  In order to avoid having to manually input every page in our <from> clause, we could change the <from> clause to simply be this:

<from>^/contact/(.*)$</from>

<to last=”true” type="permanent-redirect">/contact-us</to>

Now that we have groups of pages, what about another domain? Many website owners today may buy multiple domains but want them to point to a single web application.  If these domains are all going to be pointing to the same web application, we can use rewrites in order to direct traffic to it.  We are able to do this through the use of a condition statement:

    <rule>

        <condition name="host" operator="equal">^www.mydomain1.com$</condition>

                   <from>^/(.*)$</from>

        <to last=”true”  type="permanent-redirect">http://www.mydomain.com </to>

    </rule>

We also have the ability to add multiple conditional statements to our rule.  If we wanted to add more than just one domain to this rule, we can do that by adding the “next” attribute to our conditional statement.

    <rule>

 <condition name="host" operator="equal" next=”or”>^www.mydomain1.com$</condition>

<condition name="host" operator="equal" next=”or”>^www.mydomain2.com$</condition>

<condition name="host" operator="equal" next=”or”>^www.mydomain3.com$</condition>

<condition name="host" operator="equal" next=”or”>^www.mydomain4.com$</condition>

<condition name="host" operator="equal">^www.mydomain5.com$</condition>

                   <from>^/(.*)$</from>

                 <to last=”true” type="permanent-redirect">http://www.mydomain.com </to>

</rule>

 

Closing

While browsing the web, you may notice many of these things in practice, but never knew how they occurred.  With the knowledge presented here, you can drive traffic to the correct places and add more value to your website than you thought was possible.  Be sure to check back or subscribe (via RSS) to catch my next blog post; a more advanced dive in to the Tuckey UrlRewriteFilter. For more information you can visit http://tuckey.org/