Redirects with "Clean URLs"
tbolton — Mon, 10/25/2010 - 21:11
301 Redirects That Work With Clean URLs
With other sites, I didn't have to worry much about 301 Redirects when I had clean URLs going on. But when I started working with two Drupal paths, and both had Clean URLs (a.k.a. SEO Friendly URLs), I ran into a strange issue. So here is how I solved it.
Apache has a "mod_rewrite" module, which can rewrite URLs in a number of ways. You can use this to redirect in different ways too. It's very powerful, and extremely handy. It is also extremely dangerous, since with just a single line of code, you can replicate an entire website.
Well, anyway, the typical 301 Redirect is something like this:
And that is wonderful, however, sometimes we need to do something a little more elaborate in order to take care of all of our needs. If we are currently implementing Clean URLs, then we're already doing some URL rewriting, whether we know it or not. Drupal has been doing this natively for a while now. So, with that in mind, we can't just start throwing in 301 Redirects without first knowing what we're doing.
When we put a "typical" 301 Redirect in the .htaccess file (where all of our 301 Redirects are going to go), we might get something to the effect of the redirecting page timing out because the redirects cannot resolve properly, or in other words, it gets lost in an infinite loop of redirects.
That's not good.
That is definitely not good when you have renamed your pages or their paths so that they have better context. For my example, I changed the path from "content/website" to "portfolio/website", that way Google, Bing, Yahoo!, Lycos, etc., would all know the context of that page a little more.
So what do we do? Well, we read up on our Apache Mod_Rewrite directive, that's what we do! When we read up on it, we will discover the "RewriteRule" directive. This directive is proclaimed to be a rewriting "workhorse". And we all know, you trust the things you cherish with a workhorse, not a stallion. A stallion is unpredictable, wild, and untamed. A workhorse is broken in, predictable, full of integrity. That's what you want to work with, something that you know will work.
One of the powerful things about RewriteRule is that you can use RegEx with it. So you can define your redirects explicitly, and they will work just fine. Here's an example
So here's one of the things we need to keep our eye on: [R=301,L].
The R=301 tells Apache, "We're doing a 301 Redirect" and the "L" tells Apache, "This is the LAST rewrite rule, after this one, we're finished, so use this product."
And that's it! It's that simple. For every Clean URL that you wish you rewrite, instead of trying 500 different ways to redirect using the trusty old "Redirect 301", try the RewriteRule, you might find you've made a new friend. :)
Cheers!
- Login to post comments
