Community Documentation

Clean URLs with Apache 2 on Ubuntu

Last updated June 26, 2011. Created by mike stewart on April 7, 2007.
Edited by dbinoj, ghoti, EvanDonovan, iimitk. Log in to edit this page.

There are two methods for setting up Drupal 5.x/6.x with Apache on Ubuntu. The first (preferred) method edits the virtual host configuration, which is the default setup on Ubuntu (even for a single-site web server). The second edits the main apache2.conf, which is typical for an older setup.

Step 1 - Method A: "Virtual Host" Setup

First, from the Linux command line, enable the rewrite module for apache with this command:

sudo a2enmod rewrite

You can check to see if this worked by running:

apache2ctl -M

and seeing if it is on the list.

Next, use an editor (such as nano) to edit the appropriate Apache configuration file for your Drupal site in the /etc/apache2/sites-available/ directory. For a single site, the file is /etc/apache2/sites-available/default; if you have multiple sites, the file names should reflect the names of the sites to which they refer. Thus, to edit the default site configuration, use

sudo nano /etc/apache2/sites-available/default

Look for the Directory section referring to the folder where your Drupal site lives (in /etc/apache2/sites-available/default, this is typically <Directory /var/www>), and change the line:

AllowOverride None to AllowOverride All

(This directive permits an .htaccess file, such as Drupal's, to be used to override Apache's default settings, and is necessary to allow the URL rewriting to work. See https://help.ubuntu.com/community/EnablingUseOfApacheHtaccessFiles for more information).

Save this file and then reload Apache as follows:

sudo /etc/init.d/apache2 reload

Subdomain Setup

Instead of creating multiple virtual host files, you can create one virtual host file that uses a wildcard in the ServerAlias. Both a simple multi-site Drupal setup and multiple Drupal versions can run this way, if the different subdomains are defined for each site in settings.php.

Consider the following and modify your configuration file to fit your needs.

  1. http://myproject.dr5.example/
  2. http://myproject.dr6.example/
  3. http://myproject2.dr6.example/

Here is a partial listing of a virtual host configuration file that would support the last two lines in the above example. Note this is not intended to be a COMPLETE configuration file, but rather provide guidance for your development setup.

<VirtualHost *>
DocumentRoot "/www/Dr6"
ServerName example
ServerAlias *.dr6.example

<Directory "/www/Dr6">
AllowOverride All
</Directory>

Edit & save your config file to suit your development needs. Assuming the site is already enabled, then reload Apache.

Step 1 - Method B: apache2.conf

In Apache version 2, httpd.conf has been deprecated and the new file is located at:
/etc/apache2/apache2.conf.

Thus, it's no longer necessary to do the following in httpd.conf to enable the rewrite module (mod_rewrite):

LoadModule rewrite_module modules/mod_rewrite.so
AddModule mod_rewrite.c

Simply run the following from the Linux command line:
sudo a2enmod rewrite

To disable the module you can run:
sudo a2dismod rewrite
(Note that this would cause clean URLs to break.)

Once mod_rewrite is enabled, open apache2.conf in a text editor. Note that it will probably be read-only, so you will need sudo privileges to edit it. Use a command such as:
sudo nano /etc/apache2/apache2.conf

Find where the sections are in your apache2.conf and add another one for your Drupal site similar to this:

<Directory /var/www/your_drupal_site>
    AllowOverride All
</Directory>

After you edit apache2.conf as listed above, you need to restart the server by:
sudo /etc/init.d/apache2 reload

Step 1 - Method C: Add Rewrite Rules Directly to Virtual Host or apache2.conf

If you do not wish to allow .htaccess overrides, you can add the rewrite rules directly to a virtual host file or apache2.conf. The following should work:

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

This can provide slightly faster server performance since Apache will not look in every directory for an .htaccess file.

Note that, for proper security, you will need to add in the rules from the Drupal files directory's .htaccess file as well.

Debugging Rewrite Issues

If you are having problems with getting your rewrite to work you can set Apache to log rewrite errors. To do that add this to the end of /etc/apache2/apache2.conf:

RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 3

Level 0 does no logging. Level 9 logs everything. Choose the level necessary for resolving your issue.

Security Warning: Make sure to either remove or comment the logging code out when finished, or else put the log file in a directory that can't be read by normal users (such as /var/log/apache2). If this is not done, it can result in a security breach. Also, note that rewrite logging adds somewhat to server load, and can easily generate large amounts of output not needed on a production server.

Step 2: Enable Clean URLs

Now go to http://yoursite.com/?q=admin/settings/clean-urls, and run the test for "Clean URLs" (In Drupal 4.6 - 5.x this is buried in the paragraph explaining what "clean urls" are).

Then, select the radio button to set clean URLs to "enabled" and submit the form. You should now be able to access your site using URLs without the query string in them.

Note: In Drupal 7.2, if still you are having issues with Clean URLs, and your site is in maintenance mode, turn off maintenance mode while enabling clean URLs.

Comments

solution for ubuntu gutsy

Here is what I did to have clean urls with ubuntu gutsy:
a2enmod rewrite

and then added the following lines before line:
<Directory /var/www/>
in '/etc/apache2/sites-available/default':

<Directory /var/www/drupal>
AllowOverride all
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+\..+)$ [NC]
RewriteCond %{HTTP_HOST} !^.+\..+\..+$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^files/(.*)$ sites/%1/files/$1 [L]
RewriteRule ^files/(.*)$ sites/%1/files/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
</Directory>

ADVICE: I'm having problems with apache dir mod using this configuration since now index.php has priority over index.html.
Hope to solve it soon

rewrite.conf file did not exist

In trying to puzzle out why this wasn't working for me on Ubuntu Server 7.10, I discovered that the file /etc/apache2/modules-available/rewrite.load was there but the file /etc/apache2/modules-available/rewrite.conf did not exist. I could use the command 'sudo a2enmod rewrite' and the rewrite mod would 'look' enabled, but clean-urls would still not function because there was no .conf file for the rewrite mod.

I created one with nano, added the text...

RewriteEngine On

...to it, and saved it to /etc/apache2/modules-available/rewrite.conf.

Then, I went to /etc/apache2/sites/sites-available/default and changed both the 'Directory /' and 'Directory /var/www/' AllowOverride values from 'None' to 'All', used a2enmod to enable the rewrite mod, restarted Apache2, and I was then able to turn on Clean URLs in my Drupal install.

Cannot get it to enable in Ubuntu 8.10

I'm running Ubuntu 8.10 on Virtual box and use Drupal 6.10. I followed the directions from all posts but I couldn't enable clear URLs.

1) Ran sudo a2enmod rewrite. Module now found in mods-enabled directory. Also, the mod shows when I run apache2ctl -M

2) Updated .../sites-enabled/000-default

  <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName ubuntusrv
        ServerAlias ubuntusrv

        DocumentRoot /var/www/
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <Directory /var/www/>
                RewriteEngine on
                RewriteBase /
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

3) Created rewrite.conf file in ../mods-available as stated in previous comment.

I'm out of ideas on how to get clean url to enable. The radio button is still greyed out.

Assistance will be appreciated :) Thanks

UPDATE:
Err, got it to work finally. Modified as follows:

        <Directory /var/www/>
        <IfModule mod_rewrite.c>
                RewriteEngine on
                RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
        </IfModule>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

Thank you!

Every now and then until now I try to get this working and fail. This actually worked! Thank you

PHP

I inherited an Ubuntu box with PHP installed but missing the rewrite.conf (as noted above) and also the line:

AddType application/x-httpd-php .php3 .php

which I added to httpd.conf. Without it, apache wouldn't interpret php files as php files.

André Angelantoni
Founder, PostPeakLiving.com

Ubuntu 8.04 - Apache2

Method #2 works perfectly!

Here's a little visual helper from my /etc/apache2/apache2.conf file, notice the added line:
snippet start...
# The internationalized error documents require mod_alias, mod_include
# and mod_negotiation. To activate them, uncomment the following 30 lines.

# Alias /error/ "/usr/share/apache2/error/"
#
#
# AllowOverride None
# Options IncludesNoExec
# AddOutputFilter Includes html
# AddHandler type-map var
# Order allow,deny
# Allow from all
# LanguagePriority en cs de es fr it nl sv pt-br ro
# ForceLanguagePriority Prefer Fallback
#

<Directory /var/www/drupal_website_install> 
    AllowOverride all                 
</Directory>

#
# ErrorDocument 400 /error/HTTP_BAD_REQUEST.html.var
# ErrorDocument 401 /error/HTTP_UNAUTHORIZED.html.var
# ErrorDocument 403 /error/HTTP_FORBIDDEN.html.var
# ErrorDocument 404 /error/HTTP_NOT_FOUND.html.var
# ErrorDocument 405 /error/HTTP_METHOD_NOT_ALLOWED.html.var
# ErrorDocument 408 /error/HTTP_REQUEST_TIME_OUT.html.var
# ErrorDocument 410 /error/HTTP_GONE.html.var
# ErrorDocument 411 /error/HTTP_LENGTH_REQUIRED.html.var
# ErrorDocument 412 /error/HTTP_PRECONDITION_FAILED.html.var
# ErrorDocument 413 /error/HTTP_REQUEST_ENTITY_TOO_LARGE.html.var
# ErrorDocument 414 /error/HTTP_REQUEST_URI_TOO_LARGE.html.var
# ErrorDocument 415 /error/HTTP_UNSUPPORTED_MEDIA_TYPE.html.var
# ErrorDocument 500 /error/HTTP_INTERNAL_SERVER_ERROR.html.var
# ErrorDocument 501 /error/HTTP_NOT_IMPLEMENTED.html.var
# ErrorDocument 502 /error/HTTP_BAD_GATEWAY.html.var
# ErrorDocument 503 /error/HTTP_SERVICE_UNAVAILABLE.html.var
# ErrorDocument 506 /error/HTTP_VARIANT_ALSO_VARIES.html.var

# Include of directories ignores editors' and dpkg's backup files,
# see README.Debian for details.

# Include generic snippets of statements
Include /etc/apache2/conf.d/

# Include the virtual host configurations:
Include /etc/apache2/sites-enabled/
...snippet end.

Mike Sayre
BoonJack Media

Enable rewrite in Kubuntu 9.10 Karmic Koala

I resolved my problam with the configurantion bellow:

Acess in terminal code line: /etc/apache2/sites-available$ sudo nano default

And write this in to default file.

<Directory /var/www/drupal>
AllowOverride all
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+\..+)$ [NC]
RewriteCond %{HTTP_HOST} !^.+\..+\..+$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^files/(.*)$ sites/%1/files/$1 [L]
RewriteRule ^files/(.*)$ sites/%1/files/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
</Directory>

Best Regards,
Tks

__________________________________
Aguinaldo Alves
Maringá, Paraná, Brasil.

Finally got it to work.

Hello, I'm running Ubuntu 10.4 and installed the "drupal" package from the Synaptic package manager. I couldn't figure out how to get clean urls working after going through the instructions above.

You really only need to do 2 things and restart. (Also, remember to undo all the other changes that did not work!). I will say "Problems with rewrite" lines above did help show I was rewriting incorrectly but at least I was rewriting!

Step 1:
# Enable the rewrite module
phecht@phecht-desktop:/etc/apache2$ sudo a2enmod rewrite
[sudo] password for phecht:
Module rewrite already enabled

Step 2:
Edit the /etc/drupal/6/apache.conf and add the emphasis lines below.
Alias /drupal6 /usr/share/drupal6

<Directory /usr/share/drupal6/>
Options +FollowSymLinks
AllowOverride All
order allow,deny
allow from all
#Attempt to get clean urls for Drupal. Failed.
RewriteEngine on
RewriteBase /drupal6
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

</Directory>

Restart:
Restart the server via:
sudo /etc/init.d/apache2 restart

Still having trouble

I have basically tried all of these with no success. I made the conf file for rewrite, tried adding the module checks in the site conf file but nothing is working.

Here is the relevant part apache2 sites conf file for the site

<VirtualHost *:80>
DocumentRoot /var/www/mywebsite.com/htdocs
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <Directory /var/www/mywebsite.com/htdocs>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

It's ubuntu server 9.10 if that matters and there are several virtual sites on the server and I've gotten clean urls to work on a simpler setup by just putting this code in the apache2.conf file. But when I tried the same thing for this site and server by changing the directory path it didn't work.

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

So I'm officially lost, anyone have any ideas?

RewriteBase might be the issue

Hello,

It looks like your RewriteBase is set to "/". Try setting it to /mywebsite.com/htdocs based on what you posted. I have had that problem as well. If that doesn't work try to log the rewrite to a log as specified in the main area above.

This is one of the most frustrating things about Drupal. Not the only one, just the most!!!

Pete

I had the same problem with

I had the same problem with the drupal6 package of ubuntu lucid. The solution I used is here:

https://bugs.launchpad.net/ubuntu/+source/drupal6/+bug/371187?comments=all

Thanks

Thanks for the solution

Multisite via Virtual Hosts

My main drupal install lives in /var/www/html/main and I am trying to get my multisite working but I keep getting this error when trying to access the new site:

<strong>Forbidden</strong>

You don't have permission to access /main/site/test3/ on this server.

Apache/2.2.3 (Red Hat) Server at localhost Port 80

The crazy thing is, if I move drupal into the root dir (eg /var/www/html/) everything works just fine.

Found lots of tutorials but not seem to work for me.

Thank you.
C

This worked for me

I was actually trying to fix a permalink in Wordpress and tried everything in my .htaccess and apache config. Then came across this post. As soon as I enabled mod_rewrite everything worked fine.

Thanks
Simon

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 5.x, Drupal 6.x, Drupal 7.x
Audience
Site administrators
Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here