Search the blog

A standard 301 direct in an Apache .htaccess file looks like this:

Redirect 301 /old-folder/old-page.html

The problem is the above syntax requires the destination URL be absolute. This can be a pain if you want to test your redirects on a test site. Someone please contact me and correct me if I’m wrong but using Redirect 301 I’ve not found a way to make something like this work:

Redirect 301 /old-folder/old-page.html    /new-folder/new-page.html

Instead, what you need to do is use the rewriting engine. First, switch it on.

RewriteEngine on

Then write your redirects like this:

RewriteRule ^old-folder/old-page.html$    /new-folder/new-page.html [R=301,L]

A few points:

R=301 makes it a 301 redirect and the L means if matched, no further rules will be processed.

While the above allows you to have relative redirects on your site note that it does use the Apache’s rewrite engine which is more computationally expensive than a standard Redirect 301. Rewrite rules are more for regular expressions where you write one rule that applies to multiple pages. Still, unless you have huge amounts of redirects doing it this way should be no problem.

Tim Bennett is a Leeds-based web designer from Yorkshire. He has a First Class Honours degree in Computing from Leeds Metropolitan University and currently runs his own one-man web design company, Texelate.