Apache 2 configuration of clean URLs on Debian

If you're running Apache2 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.

 
 

Drupal is a registered trademark of Dries Buytaert.