Configuring 301 redirect using Apache and htaccess for SEO in Drupal
https://www.zyxware.com/sites/default/files/styles/user_image/public/pictures/zyxlogo.png?itok=J9t-ZEoU
BY webmaster
11 years ago
Web-Development
4 comments comment

Most webmasters do not realize this, but a lot of the content on lot of websites can be accessed from multiple URLs. 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 Apache .htaccess file and the process is the same for a Drupal installation also.

The article is relevant only to those webservers running Apache. There are two ways in which you can issue a 301 redirect using .htaccess on an Apache Webserver. One is using a mod_alias Redirect statement and the other is using the mod_rewrite Rewrite statements. Redirect statements take paths and assign corresponding paths to be redirected to while rewrite statements take regular expression patterns and associate paths where matched urls have to be redirected to.

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 ie with or without the www part.

# If you want the site to be accessed WITH the www. only, adapt and
# uncomment the following:
# RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
# RewriteRule .* http://www.example.com/ [L,R=301]
#
# If you want the site to be accessed only WITHOUT the www. prefix, adapt
# and uncomment the following:
# RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
# RewriteRule .* http://example.com/ [L,R=301]

There is however a problem with the above redirections in that they lose the rest of the URL. Say for example if you try to access example.com/somepath and your configuration is to use the www prefix the user will be redirected to www.example.com but the somepath part will be lost. This can easily be rectified by the following small change in the Rewrite Rule

# For the first option you can use the following rewrite rule
# RewriteRule (.+) http://www.example.com/$1 [L,R=301]
#
# whereas for the second you can use the following rewrite rule
# RewriteRule (.+) http://example.com/$1 [L,R=301]

These modifications will ensure that the user will be redirected to the correct page with or without the www prefix and without losing on your page ranks. So make the necessary modifications and make sure that you can access the pages to your site using only one version of the URL. The above configuration of .htaccess should be used not just for Drupal websites but for all websites. If however your problem is the reverse, ie you don't have pages corresponding to the URLs that the user typed, ie if you are getting lot of 404 errors (page not found errors) and users are thrown to the default Page not found page, you can try installing the Drupal Search404 Module to search for the keywords in the URL and show relevant results to the user.

You can read more about apache mod_alias and apache mod_rewrite at the apache official documentation Apache mod_rewrite and Apache mod_alias


RELATED ARTICLE

/themes/custom/zyxpro_light/images/placeholder.png
close

on 09th April 2007 / by webmaster
Freelance projects fall into a separate category of projects in that they target independent deve Community Projects Freelance Open Source Web Development Leave a reply Your email address will not be published. Required fields are marked * Shrinivasan (not verified) access_time 22 Nov 2019 - 04:49 Guys. Great Collection and a great site. It is really impressive to know that there a numerous FOSS tools for web contents. Thanks. Keep Growing. Anonymous (not verified) access_time 22 Nov 2019 - 04:49 In reply to Great collection by Shrinivasan (not verified) Great Collection and a great site!!! Thanks! Silvia (not verified) access_time 22 Nov 2019 - 04:49 Hi, nice work. I also love to use WordPress when setting up a new blog. Greetings JungleDirector (not verified) access_time 22 Nov 2019 - 04:49 Subj. Anybody have? I'ts better, if you have XRumer 5.04... Gimme link Thanks. Ingo (not verified) access_time 22 Nov 2019 - 04:49 Hi, this is a listing of open source software was looking for my new project Thx a lot Pagination Current page 1 Page 2 Page 3 Page 4 Page 5 Next page Next › Last page Last » Add new comment
Leave a reply
Your email address will not be published. Required fields are marked *

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type='1 A I'> <li> <dl> <dt> <dd> <h2 id='jump-*'> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

author-image
Anonymous (not verified)
access_time 22 Nov 2019 - 12:38
Hello. I'm trying to make example.com show as www.example.com, and I'm running into difficulties. I'm on Apache 2.0 and using the following lines in my httpd.conf file: RewriteEngine on RewriteCond %{HTTP_HOST} ^xxxxxxxxxx\.com$ [NC] RewriteRule ^(.*)$ http://www.xxxxxxxxxx.com/$1 [L,R=301] When I go to http://xxxxxxxxxx.com, I get http://www.xxxxxxxxxx.com (as expected). However, when I go to http://xxxxxxxxxx.com/node/1 (it's a Drupal site), I get a 404 thrown and the URL changes to xxxxxxxxxx.com/var/www/drupal/node/1. Same thing with www.xxxxxxxxxx.com/node/1. Any suggestions? I want to run without Drupal's .htaccess file (instead incorporating these calls into my httpd.conf file).

author-image

I think the problem is with the base path which results in the redirection to /var/www/ part. The best approach I would think is to start with drupal htaccess and then strip out parts and move to httpd


author-image
SNVC (not verified)
access_time 22 Nov 2019 - 12:38

This is definitely a good guide. Thanks for this.


author-image
access_time 22 Nov 2019 - 12:38

This is really nice and helpful.