Setting Up Clean URL’s for Drupal With .htaccess

By Dev Team August 17th, 2008

By default, Drupal passes path arguments to itself via its dynamically generated URLs. This results in URLs that look like “http://www.example.com/?q=node/83.” This can make URLs hard to read and it prevents some search engines from indexing the pages with these URLs. Research suggests this may not be as great an issue for some of the major search engines as it once was, however it is worth noting the recommendation from Google’s webmaster guidelines stating:

If you decide to use dynamic pages (i.e., the URL contains a “?” character), be aware that not every search engine spider crawls dynamic pages as well as static pages. It helps to keep the parameters short and the number of them few.

You can tell Drupal to use “clean URLs”, eliminating the “?q=” in internal URLs. Note that this works only for Apache servers which have the mod_rewrite Apache module configured and mod_rewrite enabled in httpd.conf configuration file.

Sometimes when you’re setting up a new install of Drupal CMS, if you want Clean URL’s enabled, you might encounter a situation where the installer (or configuration manager) says your server is not setup to handle Clean URL’s. If you don’t have access to Apache’s httpd.con configuration file (as in shared hosting scenarios), you can easily use .htaccess to fix this issue.

All you have to so is create an .htaccess file in your install’s root directory with the following directives

#
# Apache/PHP/Drupal settings:
## Protect files and directories from prying eyes.

Order allow,deny
# Don’t show directory listings for URLs which map to a directory.
Options -Indexes

# Follow symbolic links in this directory.
Options +FollowSymLinks

# Make Drupal handle any 404 errors.
ErrorDocument 404 /index.php

# Force simple error message for requests for non-existent favicon.ico.

ErrorDocument 404 “The requested file favicon.ico was not found.

# Set the default handler.
DirectoryIndex index.php

# Override PHP settings. More in sites/default/settings.php
# but the following cannot be changed at runtime.

# PHP 5, Apache 1 and 2.

php_value magic_quotes_gpc 0
php_value register_globals 0
php_value session.auto_start 0
php_value mbstring.http_input pass
php_value mbstring.http_output pass
php_value mbstring.encoding_translation 0

# Requires mod_expires to be enabled.

# Enable expirations.
ExpiresActive On

# Cache all files for 2 weeks after access (A).
ExpiresDefault A1209600

# Do not cache dynamically generated pages.
ExpiresByType text/html A1
# Various rewrite rules.

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 /

# 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]
# $Id: .htaccess,v 1.93 2008/06/28 19:48:21 dries Exp $

That’s all there is to it !!

Tags: , ,

This entry was posted on Sunday, August 17th, 2008 at 12:49 pm and is filed under Drupal CMS. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Setting Up Clean URL’s for Drupal With .htaccess”

  1. Setting Up Clean URL’s for Drupal With .htaccess Says:

    […] Go to the author’s original blog: Setting Up Clean URL’s for Drupal With .htaccess […]

Leave a Reply