Creating 301 redirects for Drupal

Laura's picture
Posted 26 November 2005 - 11:54am by Laura
Drupal

With the recent Jagger update to Google's search algorithms (more on this in a separate post), we've had more inquiries from website owners who are concerned about their sudden loss of PageRank and wish to port their sites over to Drupal, but without risking a further hit on their site rankings through 404 errors. After all, it would be silly to seek out the myriad SEO advantages of a Drupal-powered website only to have search engines suddenly unable to find one's existing pages with hard-won search engine results.

While Drupal's built-in URL redirect features can seamlessly give any page any designated URL, such an approach might not be preferable if the existing pages have file suffixes or even paths with un-optimized paths.

The natural way to rectify this is by using 301 redirects in the .htaccess file. However, the conventional approach won't work with Drupal; special syntax is required to achieve proper 301 redirects.

As a member of the Drupal community, and in the spirit of open source, I share with you the how-to copy I contributed to the Drupal handbook:


If you are porting over an existing website to Drupal, one consideration is how you redirect the old page URLs to the appropriate pages on the Drupal version of the site. If you don't want to create custom rewrite paths within Drupal for those nodes -- or perhaps cannot due to clean URLs or filename suffixes -- then 301 redirects are considered the best way to handle redirected pages, for they inform search engines to update their databases with the new paths. This way, you should not risk your search engine pagerank or lose site visitors with 404 "not found" errors.

However, 301 redirects cannot be done using the common approach. Yet establishing 301 redirects is quite easy, provided you have mod_rewrite enabled in your .htaccess file.

How to create 301 redirects in Drupal

Edit your .htaccess file in a text editor. [Note: Be sure to save the file in "UTF-8" format.]

In the file, you will find the commands:

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on

  # Modify the RewriteBase if you are using Drupal in a subdirectory and
  # the rewrite rules are not working properly.
  #RewriteBase /drupal

RewriteBase /

Immediately after that code -- and before the Drupal-provided "Rewrite old-style URLs" commands -- add your rewrite rules using the following format:

#custom redirects

RewriteRule ^/old/URL/path$ http://yourdomain.com/new/path [R=301,L]

#end custom redirects

Note the convention: The old path is simply the path off the root. The new path is the full path, including the domain. The [R=301,L] code is the 301 redirect instruction. (axbom notes: "The 301 tells browsers and spiders it is a permanent redirect, and the L ensures that no other rewrites are processed on the URL before it reaches Drupal; Hence place this code above Drupal 's own URL rewrite, but below the command RewriteEngine on.")

If you have more paths to add, insert the rewrite commands as their own line as above.

For more information, see the forum thread at http://drupal.org/node/16084 [from where I drew this information]


Comments


14 August 2006 - 12:29pm

Hi Laura,
I'm missing the "RewriteBase /" in your first snippet above. That might be my problem with getting 301 permanent redirects to work correctly on a couple of Drupal sites.
I was curious to see if you knew how to redirect something like this:

/cg--bin/* (and everything within it)
to
/ (home page)

we also have another one called /main/ that needs redirected.

We are also using pathauto. Thanks for any help.

PS - What kind of Captcha do you use? I like it.



14 August 2006 - 4:51pm
Laura's picture
Laura says:

RewriteRule ^[old-path](.+)$ http://domain.com/[new-path]$1 [R=301,L]

I'm not a *nix guru, but this has worked for me in the past. That would make yours:

RewriteRule ^cgi-bin/(.+)$ http://domain.com/$1 [R=301,L]

You'll definitely want to test this first.

The captcha is from the contributed modules on Drupal.org.



22 May 2007 - 9:21am

Hi Laura,
i tested it --> function perfect!
Thx!
I downloaded captcha module for drupal, will test it :)
Thx for tip!
Greets from Germany
Freeware Eugen



17 August 2007 - 1:21am
Matt C says:

Brilliant!
I've been searching for hours to try and fix this (I was trying to get a php page to do the processing, but couldn't get drupal to send me there).
Thank you so much.



27 December 2007 - 10:09am
Laura's picture
Laura says:

Yes you can just redirect those specific posts to URLs at another domain.