301 Redirects in Drupal
301 or permanent redirects are useful if the content of your website has permanently moved to a new URL. This happens more often than you might imagine and a 301 redirect is a great way to send users unknowingly to the new location and to notify Search Engines of the new location of old content.
This way you will keep your Page Rank and Search Engine standing while allowing your URL to change. If you do not use some sort of redirect then your old URL will still appear in searches sending users to a 404 File Not Found page, thus having your URL disappear from the results not to mention frustrating potential customers.
There are a couple different ways to add 301 redirects to your site. There is the old fashion way, used before there was a module on how to add redirects using the .htaccess file. Then, people created modules to make the process easier. There are 3 different modules you need to know about: Global Redirect, Redirect, and Path Redirect Import. We will cover what each of the modules do, as well as dependencies.
Drupal Global Redirect Module
Configuration Page: admin/config/system/globalredirect
Quick Overview of Configuration Features:
- Non-clean urls to clean urls
- Remove trailing slash
- Language path checking
- Add canonical link to each page
- Front page redirect handler
- Access checking before redirect
- Deslash and non-clean to clean URLs – helps remove common instances of duplicate URLs.
- Remove Trailing Zero Argument – not necessary for Drupal 7. If youre still on D6, we hope you have a migration plan in the works or at least on the horizon.
- Menu Access Checking – will make sure the user has access to the redirect destination before redirecting. Usually left off but you may wish to enable it if youre trying to hide URLs from people who dont have access.
- Case sensitive URL checking – another easy fix to a common duplicate content issue.
- Language Path Checking – useful for multilingual sites to redirect to the proper language.
- Add Canonical Link – helps avoid duplicate content using a the canonical link tag.
- Set Content Location Header – avoids duplicate content that may occur if a piece of content exists in multiple places.
- Taxonomy Term Path Handler – uses the built in Drupal hook to make sure redirects to taxonomy terms are handled properly.
- Frontpage Redirect Handler – any time the home page is rendered it will redirect to the canonical version.
- Ignore Admin Path – an override to make sure no admin paths are redirected.
Drupal Redirect Module
Configuration Page: admin/config/search/redirect
Quick Overview of Configuration Features:
- Automatically redirect when URLs change
- Allows you to fix 404 pages
- View list of redirects
- Add single redirect
- Bulk upload redirect files with
First Tab – List (of redirects)
Here you can see all redirects, to update or delete if necessary. You also have the ability to add a single redirect.
Second Tab – Fix 404 Pages
A list of 404 errors logged by the server. Nice to use in combination with GWMT and Open Site Explorer reports. The list is sorted by a count column showing the most visited 404 pages. If a page has a large 404 hit count its a good idea to add a redirect from that page to the proper destination.
Third Tab – Settings
- Automatically create redirects when a URL alias is changed. Without this if you changed a URL, and forgot to add a redirect the page would drop off the rankings. Important and useful feature.
- Retain query string through redirect – If a URL has data attached through a query string, this will allow the data to be passed along with the redirect.
- Set default redirect status. Will be 301 for most people.
- Allow redirects to be saved into page cache – Allows pages to be redirected, even if the page is cached.
- Delete redirects that have not been accessed for a certain period. I dont think this is necessary to do at all, but the setting is there.
Drupal Path Redirect Import
Configuration Page: admin/config/search/redirect/import
If you are taking care of doing a lot of redirects at once, life after a site launch (hopefully you started it before), this module allows you to import a .csv file of redirects. Its works with the Redirect module for Drupal 7 and the Path Redirect module for Drupal 6.
You can prepare your file in excel with the old urls in column a, new urls in column b, redirect status code in column c. Be sure to only include everything after the slash (/) after the top level domain. You can choose to include headers, or not; just be sure to identify if you did not using the check box on the import page. You can also choose to override stored redirects.
You will see on the screenshot, the Path Redirect Import module adds a tab to the Drupal 7 Redirect module configuration, making it logical and easy to find.
CSV Example Data:
The Old Fashion Way – Adding 301 Redirects via .htaccess
I dont know why you would want to add a redirect this way, when there are modules that can make it much easier. But for those of you who still want to live in 2009, here you go.
Your .htaccess file is located in the root of your Drupal installation. This file is only used on Apache server.
To create a 301 redirect using your .htaccess file find the following section of your file:
# Modify the RewriteBase if you are using Drupal in a subdirectory and
# the rewrite rules are not working properly.
Below this you may add as many 301 redirects as you like on new lines. The code for a 301 redirect is as follows:
RewriteRule ^old_file_name http://www.example.com/new-file-name [R=301,L]
Replace old_file_name with the path to the old file which you wish to have redirected and then replace http://www.example.com/new-file-name with the complete path to the new page.