Community Documentation

Clean URLs with Apache 2 on Debian

Last updated December 13, 2010. Created by Amazon on January 8, 2006.
Edited by ghoti, jubalkessler, greggles. Log in to edit this page.

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

Comments

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

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.

working!

thanks a lot!

___________________
Hédel Nuñez Bolívar
http://hedmon.com/
http://hedmon.cz/
********************

Holz, great! For others

Holz, great!

For others stuck, when i did this, i need to commnet again like this # RewriteBase /folder
and restart apache

URL alias is working for me using this

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

See the page http://drupal.org/node/781412 for an up-to-date Debian configuration for Lenny backports and Squeeze (Drupal 6.16).

pro-freedom

OK, I'm stuck here, it's my

OK, I'm stuck here, it's my first time with Debian.

- I enabled rewrite module.
- I included AllowOverride All to

<Directory "/var/www">

and to

<Directory />

But I still not able to enable clean URLs

The site should have access to .htaccess and if I'm not mistaken all the thigs to enable it are in place.

What can I be missing?

Easier than I thought

Here is how I did it:

Edit /etc/apache2/sites-available/default

And made the changes there for the AllowOverride.

Restarted apache and it started to work.

clean urls

Thanks alot. The clean urls in 3 clean steps worked.

After enabling the rewrite module,
I simply typed in:

sudo gedit /etc/apache2/sites-enabled/000-default

in the terminal. Then entered a password for the password prompt.
Then edited the file by changing:

Directory /var/www/
Options Indexes FollowSymLinks MultiViews
AllowOverride None

to

Directory /var/www/
Options Indexes FollowSymLinks MultiViews
AllowOverride All

Then restarted apache by:

sudo /etc/init.d/apache2 restart

also in the terminal.
Next i went back to localhost from my browser and then enabled clean urls

from administer > site configuration > clean urls

Save configuration. And there! It works. No more strange looking characters in my address bar.

nobody click here