How To Configuring 301 Redirect using Drupal htaccess ?

  0 comments
Share

301 redirects are useful if the content of your website has permanently moved to a new URL. A simple example would be where www.example.com and example.com leads to the same page. This is a fatal mistake in Search Engine Optimization and search engines penalize you for duplicate content. The correct configuration would be where the above two urls will lead you to the same page but example.com will redirect you to www.example.com with a 301 (Moved permanently) status which will not result in search engines penalizing the page. It is very easy to configure 301 redirects using .htaccess file.

When you install Drupal, it will automatically insert statements in the .htaccess file that will allow you to redirect www.example.com to example.com or vice versa. The only thing you have to do normally would be to uncomment the statements corresponding to your preference of accessing your domain i.e. with or without the www part. Your .htaccess file is located in the root of your Drupal installation.

To redirect all users to access the site WITH the 'www.' prefix,
(http://example.com/... will be redirected to http://www.example.com/...)
uncomment the following:

# RewriteCond %{HTTP_HOST} . # RewriteCond %{HTTP_HOST} !^www\. [NC] # RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [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 the following:

# RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] # RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]

301 (Permanent) Redirect to URLs Using the .htacess File

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. #RewriteBase /drupal

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.

Add new comment