Community Documentation

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

Last updated May 8, 2010. Created by laura s on November 26, 2005.
Edited by bertboerland, sepeck, cel4145. Log in to edit this page.

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://example.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

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=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!

-- Erik
Drupal trainer at Wizzlern

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.

psaint, how should i redirect

psaint,

how should i redirect url's with variable from an old site like: /view.php?i=[id]
to drupal's: /index.php?q=node/[id]

?

help on a redirect rule

Google web master has just flagged me for duplicate content and duplicate titles for a url that I haven't the foggiest where it came from. I would like to do a redirect whenever this url comes up because I think I may have some valuable back links going to this weird url. Heres the weird url and where I would like to have it redirected to. I am not very versed at htaccess rules but if someone gave me something I could cut and paste into my htaccess custom redirect location I could handle it. I am using drupal 6x with fusion.

weird url:

http://www.fourseasonshvacr.com/?userId=info%3Adoi%2F10.1371%2Faccount%2...

would like it redirected to:

http://www.fourseasonshvacr.com

Thanks in advance for the help

Rewrite all .html to new path

I have lot of html pages under old domain, since my domain has changed to new one, those html pages dynamically stick with the new one, But I dont have these pages except in cache, So I decide redirect them to a new url which is a proper node in drupal, and I use this command, which does work well. please free to try this.

RewriteRule ^oldpath/(.*)\.html$ http://newpath/ [R=301,L]

cheers

Bevin

maindomain versus addon

On shared hosting server, so for example my addon website becomes http://www.maindomain.com/addon.com/etc

Could I

RewriteRule ^http://www.maindomain.com/addon.com/(.*)\.html$ http://www.addon.com/ [R=301,L]

Thanks heaps in advance

Cheers

http://www.peacebids.com

Pacify the world through Peace wearable art and drupal :d

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 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.

The module is great, but . . .

I have a situation where I am converting a site from .NET. I have a slew of pages that are something like

http://ww.mysite.com/page.aspx?pageId=01
http://ww.mysite.com/page.aspx?pageId=45
http://ww.mysite.com/page.aspx?pageId=543

but I need them to be 301 redirected to something like:

http://ww.mysite.com/news/this-is-the-new-page
http://ww.mysite.com/events/i-am-writing-about-stuffs
http://ww.mysite.com/news/something-happened-today

I've set up my new content types to have a CCK field called "OldPageId" What I'd like to be able to do is handle the request for the old .NET pages, extract the pageId, look for the node in the drupal database based on that Id, and then redirect.

I could enter all redirects using that module (or in the .htaccess file), but it's like thousands of entries. I feel like this is something that should be fairly trivial . . . but I don't have enough experience with PHP or Apache or drupal to know the best way to go about this.

Any have any thoughts or know of a module that might do something like this?

I know it is possible,

I know it is possible, although im not the man to help you with it. I have been reading a drupal development book. It seems to me like the way to do this would be to create your own custom module.

This would use one of the drupal "hooks" to grab the incoming url i.e.

pseudo-code

function(mymodule_url_request_hook)
{
  if(args(0) == 'pageId' && is_numeric(args(1))
  $res = db_query('SELECT nid FROM node WHERE oldID='arg(1)')
  $node = db_result($res)
  node_load($node);
}

remembering of course to escape your url arguments for malicious input
Do an sql search on the database for "oldID", returning the new node ID if found. You then use node_load(node_id) to load the corresponding node.

Although this is a very simple pseudo-code way of stating what you have to do, it would require more research and is probably not jsut as easy as that to redirect pages but it is definitely possible.

Redirect a single page to a new URL... How to??

Ho to all!

I need to make a 301 redirect of a single (old) url to a new one.

How I have to write the rule and where i have to put in .htaccess drupal file?

I tryed with something like this:

  Redirect /reviews/old_name_of_review  /reviews/new_name_of_reviews [R=301,L]

But everytime gives me a 500 internal error and the site become unaccessible.

Thanks!
Bye!

Redirecting one Drupal page to another Drupal page

I've had the same problem, any suggestions would be really handy. I can redirect .php pages and .html pages easily, but is there a way to redirect a Drupal page (i.e. with no .html / .php on the end) to a new Drupal page by modifying the .htaccess file?

I've got pathauto installed, hence using the other module recommended isn't an option, as the 2 aren't compatible currently (though it looks like they will be soon).

Thanks!

301 ReDirect Help

I just came back in here because I spent hours a few months ago trying to fix a Not Found with a 301 redirect. I am no more savy than the next guy so I though I would show where I have had success with this on 2 drupal sites so far. Here is what my code says.

"RewriteRule ^upcoming/newest/SEO$ http://www.karmalynx.com [R=301,L]"

just substitute your sites info for mine

Here is what is posted on KarmaLynx.com. That goes into more detail.

This comes from reading many unanswered questions in the drupal.org community discussion area. I noticed that not many people were able to get their 301 redirects to work properly. I read incorrect answers many times over. Here is a quick an easy version. I have also included a 301 redirect that this site karmalynx.com uses so you can see exactly what you will need to enter into your .htaccess file to have success.

First here is what is listed in the Drupal information how to section under the title, "Redirecting specific pages to new URLs (301 redirects in Drupal)"


Here is the information from the Drupal Page:

    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


Next the instructions say:

    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.

This can be confusing since they explained it by saying "old path is simply the path off the root." So here is where I believe a lot of people will be helped:

First you want to find this section in your .htaccess file: (this assumes you have made no changes yet)

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

      # If your site can be accessed both with and without the 'www.' prefix, you
      # can use one of the following settings to redirect users to your preferred
      # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
      #
      # To redirect all users to access the site WITH the 'www.' prefix,
      # (http://example.com/... will be redirected to http://www.example.com/...)
      # adapt and uncomment the following:
      #RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
      #RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
      #
      # To redirect all users to access the site WITHOUT the 'www.' prefix,
      # (http://www.example.com/... will be redirected to http://example.com/...)
      # uncomment and adapt the following:
      # RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
      # RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

      # Modify the RewriteBase if you are using Drupal in a subdirectory or in a
      # VirtualDocumentRoot and the rewrite rules are not working properly.
      # For example if your site is at http://example.com/drupal uncomment and
      # modify the following line:
      # RewriteBase /drupal
      #
      # If your site is running in a VirtualDocumentRoot at http://example.com/,
      # uncomment the following line:
      #RewriteBase /
   


    RewriteRule ^Money$ http://www.karmalynx.com/BusinessPolitics [R=301,L]


    RewriteRule ^upcoming/newest/Computer$ http://www.karmalynx.com [R=301,L]


    RewriteRule ^upcoming/newest/SEO$ http://www.karmalynx.com [R=301,L]


    RewriteRule ^Computer/Drigg_Powered_logo_gone_and_error_in_logs$ http://www.karmalynx.com [R=301,L]




      # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_URI} !=/favicon.ico
      RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    </IfModule>

Above We have highlighted in red what you need to add to do a very simple 301 redirect. Thje green text is the "OLD/URL/path" from the instructions. The very important part to executing this redirect is to make sure you keep the " ^ " in the front and make sure you end with a " $ ". Also you want to add the Rewrite rules where we added them above. In the drupal forum, the missing " $ " really seemed to be throwing people off.

That should do it, I hope it helps someone out.

Check Out KarmaLynx.com for your Social Bookmarking needs. Built with Drupal and Sharing the Karma!

-

@SWMdave :

Brilliant, thank you!

----------------------------------------------------------------------
http://classicvinyl.biz ~ http://vinylclassics.biz ~ http://jazz.vinylclassics.biz
My YouTube Channel

Great! Thanks a lot! :D

Great!

Thanks a lot! :D

The best help ever!!!

So many assume so much about their audience. You have taken a babe in Drupal and made sense out of a difficult project for me by giving me baby steps. THANK YOU THANK YOU for taking the time to show us exactly how it should look. You should be an instructor.

Top Listed Solution works Fine in D6 2010 April

To all ye wandering souls seeking solutions to your lost pages:

It seems like there's some confusion around this important topic. I ran the top listed solution as outlined (watch the code placement) and it ran fine.

I had installed a custom redirect at the cPanel level on Hot Drupal, which dropped the redirect statement at the end of the .htaccess file which did not work. I think HD did the intelligent default thing, but Drupal sometimes doesn't like the intelligent default ;-)

This is really important when migrating to a new site when you've got old legacy links, especially if they've got high traffic!

Happy redirection.

--
Joe Golden
www.triangul.us : People, Ideas, Connections

Strange thing!

I tryed with success with path like this:

RewriteRule ^main/contact$ http://example.com/new/path [R=301,L]

But didn't work with this:

RewriteRule ^main/default.aspx\?Action=solution$ http://example.com/new/path [R=301,L]

The problem seems to be the old dinamics .aspx type of url...

Do Someone has ideas about?
Thanks!

Found a solution, with

Found a solution, with this:

RewriteCond %{THE_REQUEST} Action=solution
RewriteRule  . http://example.com/new/path [R=301,L]

Now a new question: I have a lot of page with request like "Action=somethings", how can I write a single Condition with many rule for different request?

Example, i tryed something like this but didn't works:

RewriteCond %{THE_REQUEST} Action=
RewriteRule  ^solution$ http://example.com/new/path [R=301,L]

Thanks!

Internal Server Error

Hi guys,
I'm have two drupal sites that one of them sits on the main domain directory (public_html), and the other sits on a sub domain (located on the sites/all folder). I've already tried all of the solutions mensioned here, but none of them worked for me. Every change I made to the .htaccess file is causing an "Internal Server Error", so I need to roll-back the changes.

This is what I'm using (betwin the "" tags, for my redirects:

RewriteRule ^content/%D7%A7%D7%95%D7%A8%D7%A1-%D7%90%D7%99%D7%A4%D7%95%D7%A8-%D7%9E%D7%A7%D7%A6%D7%95%D7%A2%D7%99$ http://school.breeze.co.il/content/%D7%9E%D7%97%D7%9C%D7%A7%D7%AA-%D7%A7... [R=301,L]

RewriteRule ^content/%D7%9C%D7%99%D7%9E%D7%95%D7%93-%D7%90%D7%99%D7%A4%D7%95%D7%A8-%D7%A7%D7%91%D7%95%D7%A2$ http://school.breeze.co.il/content/%D7%9E%D7%97%D7%9C%D7%A7%D7%AA-%D7%A7... [R=301,L]

I'm missing something or am I just can't get the point it should work on...
By the way this what the Error log says:
[Sun Jan 09 14:20:28 2011] [alert] [client 84.108.241.195] /home/mydomainUsername/public_html/.htaccess: Invalid command '\xef\xbb\xbf#', perhaps misspelled or defined by a module not included in the server configuration
Can somebody help me? cause I'm confused.
Thanks.

About this page

Drupal version
Drupal 4.6.x, Drupal 4.7.x

Installation guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.