Apache 2 configuration of clean URLs on Debian

Last modified: March 8, 2009 - 17:41

Note: This article needs to be updated for Debian "Etch" and "Lenny" releases. (as of March 2009)

If you're running Apache 2 on Debian stable, in order to install the rewrite module you simply need to:

# a2enmod rewrite

then restart the webserver:

# /etc/init.d/apache2 restart

then edit either /etc/apache2/sites-enabled/drupal or to your .htaccess and ensure it looks something like this:

#
# Apache/PHP/site settings:
#

# Protect files and directories from prying eyes:
<Files ~ "(\.(conf|inc|module|pl|sh|sql|theme|engine|xtmpl)|Entries|Repositories|Root|scripts|updates)$">
  order deny,allow
  deny from all
</Files>

# Set some options
Options -Indexes
Options +FollowSymLinks

# Customized server error messages:
ErrorDocument 404 /index.php

# Set the default handler to index.php:
DirectoryIndex index.php

# Overload PHP variables:
<IfModule sapi_apache2.c>
   # If you are using Apache 2, you have to use <IfModule sapi_apache2.c>
   # instead of <IfModule mod_php4.c>.
   php_value register_globals        0
   php_value track_vars              1
   php_value short_open_tag          1
   php_value magic_quotes_gpc        0
   php_value magic_quotes_runtime    0
   php_value magic_quotes_sybase     0
   php_value arg_separator.output    "&amp;"
   php_value session.cache_expire    200000
   php_value session.gc_maxlifetime  200000
   php_value session.cookie_lifetime 2000000
   php_value session.auto_start      0
   php_value session.save_handler    user
   php_value session.cache_limiter   none
   php_value allow_call_time_pass_reference  On
</IfModule>

# Various rewrite rules
<IfModule mod_rewrite.c>
  RewriteEngine on
  Options All

  # Modify the RewriteBase if you are using Drupal in a subdirectory and the
  # rewrite rules are not working properly:
  RewriteBase /drupal

  # Rewrite old-style URLS of the form 'node.php?id=x':
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{QUERY_STRING} ^id=([^&]+)$
  RewriteRule node.php index.php?q=node/view/%1 [L]

  # Rewrite old-style URLs of the form 'module.php?mod=x':
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{QUERY_STRING} ^mod=([^&]+)$
  RewriteRule module.php index.php?q=%1 [L]

  # Rewrite URLs of the form 'index.php?q=x':
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

# $Id: .htaccess,v 1.58 2004/10/09 20:41:49 dries Exp $

This is because the debian package installs the drupal "sites-enabled" virtual host, result being that drupal is accessed from http://example.com/drupal, hence the need to uncomment RewriteBase. Change it to the base that corresponds to your system.

ref: http://www.debian-administration.org/articles/136
http://drupal.org/node/14322

--
Sean K. O'Brien
CTO
Colley Graphics, LLC
http://www.colleygraphics.com

Virtual hosts

pembeci - February 16, 2006 - 17:51

I am using apache2 on debian and I first encountered some problems while following instructions here since I had a Virtual Host directive in the apache configuration for my drupal site.

In case you have a VirtualHost directive make sure it includes "AllowOverride All" and not "AllowOverride None". Secondly, I was using the "RewriteBase /my_drupal_site_directory" as described above and the clean URLs was not working. When I comment that part out they became working. I guess if you already have a VirtualHost directive for that directory you shouldn't specify the RewriteBase.

Debian - Clean URL's and Virtual Hosting

v1nce - July 16, 2006 - 23:13

Thanks to pembeci for pointing this out.

I had initially uncomment the "RewriteBase /mysite" in an .htaccess file and later updated my test server to allow virtual hosting. I was banging my head when clean url's wasn't working and I knew I was overlooking something.

So if you enable virtual hosting in apache then comment out the "RewriteBase /mysite" in you sites-enabled file or the local .htaccess file and you'll save yourself some time.

Debian - Clean URL's and subdirectories without Virtual Hosting

stgaito - December 1, 2006 - 17:26

If you run drupal in a subdirectory off your main website (say: http://www.example.com/drupal )

AND

If you follow the above instructions and uncomment the RewriteBase /drupal line in your .htaccess or http.d (or your drupal apache configuration file)

THEN you must also change the RewriteRule

FROM:

RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

TO:

RewriteRule ^drupal/(.*)$ index.php?q=$1 [L,QSA]

(It took me until I enabled RewriteLog and RewriteLogLevel 2 to understand why my clean url's weren't working.... they had the initial drupal copied after the "?q="!!! :'(

no need to change .htaccess

holz - August 19, 2007 - 09:06

after you enable rewrite module, edit /etc/apache2/sites-enabled/000-default, and change

        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride none

to

        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride all

done.

IMPORTANT

il-p2p - October 30, 2007 - 18:21

Hi,
Please note that with the method 'HOLZ' has specified I GOT 500 errors instead of 404s etc ! ...

andre`

Clean URLs not working after all your advice!

Riccard0 - February 16, 2008 - 18:39

Hi,
I activated a2enmod rewrite, configured /etc/apache2/sites-available/default as

DocumentRoot /var/www/mysite/

Options FollowSymLinks
AllowOverride All

then modified my /var/www/mysite/.htaccess as:

# Various rewrite rules.

RewriteEngine on
Options All

RewriteBase /mysite

# Rewrite current-style URLs of the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ mysite/index.php?q=$1 [L,QSA]

trying all possible combinations of:
RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
RewriteRule ^mysite/(.*)$ index.php?q=$1 [L,QSA]

and it is not working.
Please need help,
R.

URL alias is working for me using this

sourabh.iitm - September 8, 2008 - 13:30

I have updated my debian system for new softwares today and now there is no RedirectMatch line in default file located at /etc/apache2/sites-enabled/default.

It seems like apache in debian is not using any default location for 1st page like it was using before http://example.com/apache-default/

So for me clean url is working using a 3 step process

Step 1 : Use this command
a2enmod rewrite

Step 2 : Change AllowOverride none to AllowOverride All at both the places in default file located at /etc/apache2/sites-enabled/default

Step 3 : Restart apache using the command
/etc/init.d/apache2 restart

I hope this will help

URL alias is working for me using this

alvise - January 23, 2009 - 11:58

This way clean url is working for me too.
Debian GNU/Linux 5.0 (lenny)
acquia-drupal-1.2.1

No need for tweaking the .htaccess file.

HTH

Confirmed on Lenny with manual Drupal install

jhagmar - March 30, 2009 - 09:30

I can confirm that the method of sourabh.iitm works on Debian Lenny with a manual install of Drupal 6.10, although for step 2 I changed to "AllowOverride All" in /etc/apache2/sites-available/default, and only in the section:

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

You also get by with /etc/init.d/apache2 force-reload instead of /etc/init.d/apache2 restart.

Help! 000-default hides drupal sites in subdirectories of root

whatdoesitwant - July 28, 2009 - 10:53

I use Ubuntu jaunty because it is an option in VirtualBox.
The webserver is:
Apache/2.2.11 (Ubuntu) PHP/5.2.6-3ubuntu4.1 with Suhosin-Patch
I work from http://justinhileman.info/articles/a-more-secure-drupal-multisite-install

I would like to ask for your help.

I'll show you my set up first and then i'll get to my questions, the most important one of which is:
How is it that i must disable 000-default to see my Drupal sites?
This is a bit long because I want to be as clear as I can, but I'm just a themer so I'm really grasping to understand this server management stuff.

Set up
I have added several drupal installations at
/var/www/drupal6
/var/www/drupal7
/var/www/mysite

My apache2 has a default virtual site configuration for localhost that is described by
/etc/apache2/sites-enabled/000-default
(Strictly speaking 000-default is a symlink to the true configuration file /etc/apache2/sites-available/default.)
I have added the following configuration files for my own virtual sites:
/etc/apache2/sites-enabled/drupal6dev
/etc/apache2/sites-enabled/drupal7dev
/etc/apache2/sites-enabled/subdomain.drupal6dev

I have added these sites to my hosts file as
drupal6.dev
www.drupal6.dev
subdomain.drupal6.dev
drupal7.dev
www.drupal7.dev

/etc/apache2/sites-enabled/000-default
points to the physical localhost root /var/www.
the other files point at th physical directories:
var/www/drupal6
var/www/drupal7
var/www/subdomain.drupal6 respectively.

This is a snapshot of /etc/apache2/sites-enabled/000-default:

<VirtualHost *:80>
ServerName localhost
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
~
    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>
</VirtualHost>

This is a snapshot of /etc/apache2/sites-enabled/drupal6dev:

<VirtualHost *>
ServerName localhost
ServerAlias drupal6.dev
ServerAdmin ikke@drupal6.dev
DocumentRoot /var/www/drupal6
<Directory />
Options FollowSymLinks Multiviews
AllowOverride All
</Directory>
<Directory /var/www/drupal6>
Options Indexes FollowSymLinks Multiviews
DirectoryIndex index.html index.php
AllowOverride all
Order allow,deny
allow from all
</Directory>
~
</VirtualHost>

I use Drupal's default .htaccess at /var/www/drupal6.
To redirect drupal6.dev to www.drupal6.dev I have uncommented this code:

  RewriteCond %{HTTP_HOST} ^drupal6\.dev$ [NC]
  RewriteRule ^(.*)$ http://www.drupal6.dev/$1 [L,R=301]

To enable clean urls I have uncommented this code:
RewriteBase /

Questions

  1. How is it that i must disable 000-default to see my drupal sites?
  2. How do I change my configuration to let my drupal sites live side by side with 000-default?
  3. Is my .htaccess for drupal6.dev going to conflict with my subdomain.drupal6.dev?

As a bonus question:

  • Is it possible at all to cleanly redirect to my drupal sites from /var/www with a .htaccess rewrite in root, instead of using virtualhost configurations?

...of je stopt de stekker erin.

 
 

Drupal is a registered trademark of Dries Buytaert.