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:

After Friendly URL Rewrite:

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


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


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

Redirect One Old Dynamic URL to A New URL


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


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

Redirect One Domain to Another


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


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! If you have more questions about technical SEO, download our Digital Marketing Checklist.

Share this article

Facebook Icon Twitter Icon LinkedIn Icon Pinterest Icon