Redirecting specific pages to new URLs (301 redirects in Drupal)

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 Apache mod_rewrite

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]

More 301 redirects

Sutharsan - October 6, 2006 - 16:42

You have carefully rebuild your existing website in Drupal. Matched the existing path to Drupal path. With a few exceptions of course. But redirecting all those pages with hard coded redirects as above is a lot of work. Well Apache can crack this nut for you. The below .htaccess snippets are an extention to ones the above.

Lets say you have plenty of pages in your existing site like /foo/foo_detail.html and some like /bar/index.html.
In your new Drupal site this same page content is found with path like: /foo/foo_detail and /bar.

To redirect from the old to the new you place this code in your .htaccess file. Make shure to place it prior to the standard Drupal redirects or it will not work.

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

  #custom redirects

  RewriteRule (.*)/index\.html$ http://www.mysite.nl/$1 [R=301,L]
  RewriteRule (.*)\.html$ http://www.mysite.nl/$1 [R=301,L]

  #end custom redirects

Ok, the most of your redirects are fixed. Now the exceptions.
First the easy ones. The pages where the new path is nothing like the old one, must be hard coded and come first. Old: /this_is_hot_news/index.html, is now found in: /news.
Your own bloud, sweat and tears, .php files are now taken care of by Drupal modules and path. Just use the same trick ase above but now with 'php' instead of the 'html'.

The looks like:

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

  #--------------- custom redirects -----------------
  #hard coded 
  RewriteRule ^this_is_hot_news/index\.html http://www.mysite/news [R=301,L]

  RewriteRule (.*)/index\.html$ http://www.mysite.nl/$1 [R=301,L]
  RewriteRule (.*)\.html$ http://www.mysite.nl/$1 [R=301,L]

  RewriteRule (.*)/index\.php$ http://www.mysite.nl/$1 [R=301,L]
  RewriteRule (.*)\.php$ http://www.mysite.nl/$1 [R=301,L]

  #end custom redirects

Finally, you have some dynamic pages rebuilt into Drupal with URL like: /photo?series=2 and /photo?series=3, etc. Whell, this requires serious redirecting. The RewriteRule as we used above does not take the part behind the questionmark into account. The code below does the trick.

  RewriteCond %{THE_REQUEST} series=2
  RewriteRule . http://www.mysite.nl/node/75? [R=301,L]
  RewriteCond %{THE_REQUEST} series=3
  RewriteRule . http://www.mysite.nl/node/72? [R=301,L]

Note the question mark after the node number. This is needed there to clear the query string of the URL. Place this code right below the hard coded rewrites (the /news path in the example above).

Good luck. I hope this saves you the time it took me to redirect my first site!

Rewrite rules for dynamic pages

psaint - November 5, 2007 - 12:09

Actually I've tried Sutharsan approach and it did not work for me. To make it useful I had to use sth like this:

RewriteCond %{QUERY_STRING} ^q=/node/subnode/file_name.html$
RewriteRule ^$ http://www.mysite.com/node/100? [R=301,L]

Breaking this down we will get:
%{QUERY_STRING} - connection and request variable in Apache which keeps the query part of url i.e. for the following url http://www.mysite.com/?q=oneOfPages.html
it will return everyting after 'q' mark.

^ - means starts with (in RegEx)
$ - ends with (in RegEx)
^$ - empty string (in RegEx)
[R=301] - force external redirection (301 specifies what HTTP response code will be returned - default is 302 - moved temporarily)
[L] - stop processing rewrite rules on this rule (we want to avoid any other rewrite commands to be used on our newly generated URL)

Important
Remember about '?' at the end of url to which you are redirecting. If you don't include this trailing '?' all query you from the old url will be appended to new url and you will get invalid redirection.

Hope this will help some of you.

 
 

Drupal is a registered trademark of Dries Buytaert.