Web Design

Drupal 7 Clean URLs in Ubuntu

This is a problem I've solved many times, but I keep forgetting to write it down.  The problem is: You install Drupal on a new Linux server running Ubuntu.  Clean urls don't work.

Solution: Actually, there are several.  This is my attempt to make it simple.

  1. LAMP: Make sure you installed the lamp stack correctly.  Follow these instructions: https://help.ubuntu.com/community/ApacheMySQLPHP
  2. Install Drupal: Make sure Drupal is installed correctly.  If you follow the official Drupal instructions for clean urls, most of them have to do with an .htaccess file not being set up correctly.  D7 comes with an .htaccess file that already has most of the settings you need, so make sure it's there.
  3. mod_rewrite: Install the Apache rewrite module: sudo a2enmod rewrite
  4. Apache config: Modify the Apache config file.  On Ubuntu it's located at /etc/apache2/apache2.conf .  Add the following:
    <Directory /var/www/>
       Options Indexes FollowSymLinks
       AllowOverride All
       Require all granted
    </Directory>
     
    <Directory /var/www/html>
       RewriteEngine on
       RewriteBase /
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
       AllowOverride All
    </Directory>
  5. Restart Apache: /etc/init.d/apache2 restart
That's it!  This has worked for years, all the way up to Ubuntu 14.04.  Does it work for you?  Leave a message in the comments.