When Friendly Turns Ugly: 301 Redirects in .htaccess with Friendly URL Rewrites

By Janet Driscoll Miller | Oct 19, 2010
More Articles by Janet


As an SEO, I have my share of frustrations with content management systems (CMS), although I find their adoption to be growing amongst many companies. Generally my frustrations stem from CMS’ lack of attention to SEO. While many have come a long way in doing a better job at prepping content and pages for SEO, there are many fundamentals still lacking in many types of systems.

Recently I had to program 301 redirects for two clients in their .htaccess files. And while this seems fairly straightforward, unless you are highly experienced with .htaccess 301 redirects, sometimes CMS .htaccess files can really throw you off. Why?

Friendly URLs

Many CMS systems, including WordPress, Magento for ecommerce, and Drupal, enable “friendly URLs” via the .htaccess file. What are friendly URLs?

Friendly URLs (or search engine friendly URLs) are a rewritten version of a given URL. They’re considered “friendly” to search engines because they remove all of the dynamic parameters from the URL string, allowing the URL to look cleaner. For example:

Before Friendly URL Rewrite:
http://www.abc.com/test.php?cat=3&page=2&productid=789&productname=pizzacutter

After Friendly URL Rewrite:
http://www.abc.com/pizzacutter

Nice, huh? This is especially helpful for ecommerce sites and sites using CMS because it cleans up all of the dynamic clutter and makes a clean, easy to read URL for the user and the search bot. Fabulous, right?

Friendly Can Be Ugly!

When you’re using .htaccess to do your 301 redirects, however, friendly can turn ugly fast. Here’s the style you’ll need to use if you’re redirecting URLs that are also being rewritten by the CMS to be friendly:

Redirect One Individual Page to Another

Example:

www.abc.com/boo.html redirects to www.abc.com/boo

Code:

RewriteRule ^boo.html$ http://www.abc.com/boo [R=301,L]

Redirect One Old Dynamic URL to A New URL

Example:

www.abc.com/test.php?cat=3&page=2&productid=789&productname=pizzacutter redirects to www.abc.com/pizzacutter

Code:

RewriteCond %{QUERY_STRING} ^(.*)productid=789$
RewriteRule ^index.php$ pizzacutter? [R=301,L]

Redirect One Domain to Another

Example:

www.abc.com redirect to www.def.com

Code:

RewriteCond %{HTTP_HOST} ^www.abc.com$ [NC]
RewriteRule ^(.*)$ http://www.def.com/$1 [L,R=301]

To do multiple domain redirects like this, use [OR]:

RewriteCond %{HTTP_HOST} ^abc.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.abc.com$ [NC]
RewriteRule ^(.*)$ http://www.def.com/$1 [L,R=301]

Happy 301 redirect programming!

Share this article

Share on LinkedIn Share on Twitter

Blog Search


Receive Daily Digital Marketing Tips

Subscribe to monthly updates from the Marketing Mojo Blog to get the latest digital marketing tips, best practices and insights - hot off the presses and straight to your inbox!

 

  • Carey Gatz

    We recently had the chance to use this tip. It cut down writing my 301 redirect time by a ton. Can’t believe how simple it is or that I didn’t think of it myself. Thanks Search-Mojo!

  • http://stock9trades.com sachin sharma

    I have tried 301 for “www” issue.

    Now I am going to change all my urls and making them more seo friendly. plz suggest .htaccess for this:

    Old URL: http://www.domain.com/page.html

    New URL: http://www.domain.com/directory/page.html

    Thanks