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.

Comments

I successfully installed Drupal 7 on Ubuntu 14.04 on my server following the steps at https://www.rosehosting.com/blog/how-to-install-drupal-7-on-ubuntu-14-04/

Then, I created a .htaccess file in the Drupal root directory with the following content:

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

Clean URLs work as expected.

Yes, but the problem is .htaccess gets overwritten every time you update Drupal core.  By putting the code in your apache settings, you avoid that problem.

Pages

clever girl