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 redirectsNote 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
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=2RewriteRule . 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
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.
Taxonomy Term Redirects
What if you change the name of a taxonomy term, and you need to redirect urls relating to the old taxonomy term to the new term?
Eg: mysite.com/dogs is changed to mysite.com/cats, where dogs and cats are taxonomy terms that are displayed via the default taxonomy term view (views module).
This is working for me...
# uncomment the following line:
RewriteBase /
#custom redirects
RewriteRule ^oldTaxonomyTermURL/(.*)$ http://mysite.com/newTaxonomyTermURL/$1 [R=301,L]
#end custom redirects
there is a module so you don't have to tweak the .htaccess
There is a module where you can enter the old pages and the new drupal path, in this way you don't have to put it in the .htaccess, and you don't have any trouble when upgrading your Drupal installation.
http://drupal.org/project/path_redirect
pathredirect and 301 requests
Hi,
ive been looking at a lot of drupal threads about 301 redirects for old links and i wondered why there is so much on .htaccess rules when there is the path_redirect module avaialble. One assumption I made is that most of these htaccess articles are written before the path-redirect module was made. But i just wanted to check that the .htaccess rules are not in some way cleaner and more SEO friendly than the path_redirect. Does this module send 301 responses directly to the browser/spider or are there any kind of intermediary responses that are not as clean as .htaccess rules.
If my verbal garbage does not make any sense i apologise, im just trying to get my head round the best way to keep alive my old static links which have been indexed by SE's. I only have a handful of pages and no user generated content (yet) so its not an issue to manually create redirects. Dont think i need pathauto as i can create alias' manually and use global-redirect to make sure they are the only links used. ps. Can the path-redirect module stop //example.com as opposed to //www.example.com or does that need a .htaccess rule?
Thanks in advance to all.