Clean URLs

By default, Drupal passes path arguments to itself via its internally generated URLs. This results in URLs that look like the following: "http://www.example.com/?q=node/83." This can make URLs hard to read and it also stops many search engines, like Google, from indexing the pages with these URLs.

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 LoadModule rewrite_module configured and mod_rewrite enabled in httpd.conf configuration file.

There are two ways to enable URL rewrites in Apache. If there is complete control of the Apache webserver clean URLs should be enabled in the httpd.conf as this has better performance and security [http://www.serverwatch.com/tutorials/article.php/3436911].

Warning. Enabling "Clean URLs" if your server is not properly configured can make it difficult to navigate back to administration pages to undo your mistake. If you find yourself in this situation, you can return to the administrative settings page by typing in the URL in the 'non-clean' form: http://www.example.com/?q=admin/settings.

Enabling clean URLs involves three steps:

  1. Enable mod_rewrite for Apache. Please talk to your web host or consult the Apache documentation [http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html] for mod_rewrite to get more information on how to do this. At a minimum, this will involve making sure that mod_rewrite is enabled for your installation of Apache. It will have to be either compiled-in or made available as a loadable module. Generally speaking, you can tell Apache to load the module by including
    LoadModule rewrite_module modules/mod_rewrite.so
    AddModule mod_rewrite.c

    in your Apache configuration file. Be sure to uncomment AddModule mod_rewrite.c. Note that this may not be the case for all distributions of *nix operating systems. Consult your distribution's documentation that came packaged with your Apache software. We also recommend disabling multiviews in Apache as they conflict with clean URLs.

  2. Edit your Apache configuration files for your site: the configuration information may be found in httpd.conf, a virtual-host-specific file, or in an .htaccess file in your Drupal installation directory. You can find this file usually in /etc/httpd/conf/httpd.conf. You may use
    find /etc -name httpd
    to find the file if it is located else where in your Unix system. The main configuration option which may need to be changed for your site is the RewriteBase. For example, if your Apache DocumentRoot is /var/www/ (i.e., /var/www/index.html is what is displayed when you point your browser at http://www.example.com/) and your Drupal installation is installed in the subdirectory /var/www/mysite/, then the RewriteBase should be set to /mysite. In some configurations setting
    RewriteBase /
    will allow mod rewrite to work.

    If you don't use the .htaccess that comes with Drupal you'll need to add some rewrite rules into your apache directory directive. Consult the .htaccess file in Drupal for examples of rules.

    <Directory /var/www/example.com>
      RewriteEngine on
      RewriteBase /
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    </Directory>

    You will also need to set the Allow Override settings in httpd.conf so that local .htaccess commands will be run for you site. If you are changing the .htaccess file in the Drupal distribution you want to set it to

    AllowOverride All
    to ensure rewrites are enabled. Read "Behind the scenes with Apache's .htacces [http://brainstormsandraves.com/archives/2005/10/09/htaccess/] for a thorough review of .htaccess. Here are samples of Apache 2 directives [http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride].
  3. You should ensure your Drupal site has the path module enabled and the correct permissions set in order to create custom URLs. You can enable the path module in administer >> modules. You can then set permissions to administer URL aliases and create URL aliases. Enable clean URLs on your administration >> settings page. First, see if you can go to the settings page on your site using clean URLs: type in the URL http://www.example.com/admin/settings (where www.example.com is replaced by your hostname). If you get no errors and get to the same page you would have gotten to by clicking on "administer" then "settings" , then you know that you have setup the ReWriteRule rules successfully and can then click the Yes checkbox for "Clean URLs:" . If you have problem you can read the instructions to unset clean URLs [http://drupal.org/node/5590]. If you If you still have problems with clean URLs you can set the Drupal Settings.php $conf['clean_url']=1;.

Note: The standard Drupal installation contains a sample .htaccess file which supports clean URLs. It is easy to miss copying this file, because of the leading "dot".

Note Regarding MultiViews: The Apache webserver supports a feature called "MultiViews" (more generally: "Content Negotiation"), which allows navigation to your pages without the need for file extensions. For instance, if you had a file called "evaluation.txt", a MultiViews-enabled site could access this file with the URL "example.com/evaluation". While MultiViews can be a handy feature when used knowingly, they can cause problems when Drupal's Clean URLs are enabled. Unless you know what you're doing, you should consider disabling MultiViews if you plan to use the clean URLs feature of Drupal. Be aware that there's a good chance that MultiViews is already disabled, however; it's not enabled in a default Apache installation. Consult the Apache documentation for further information about MultiViews [http://httpd.apache.org/docs/mod/core.html#options].