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
Web Development
Drupal
SEO
Drupal Planet
Leave a reply
Your email address will not be published. Required fields are marker *
Anonymous (not verified)
access_time
21 Feb 2019 - 13:33
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).
webmaster
access_time
21 Feb 2019 - 13:33
In reply to Rewrite including filesystem path by Anonymous (not verified)
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
SNVC (not verified)
access_time
21 Feb 2019 - 13:33
This is definitely a good guide. Thanks for this.
wellyson
access_time
21 Feb 2019 - 13:33
This is really nice and helpful.
Add new comment
comments comment