How to Configure Apache2 to Allow Drupal to Rewrite Paths

 by jakedimare- September 10, 2011

In this Rackspace Recipe we’ll configure a ‘sites-available’ file in Apache2 in order to allow Drupal (or WordPress) to rewrite paths. After installing a Drupal site on a Rackspace Cloud server for theĀ  first time, chances are you’ll notice it is not possible to enable the Clean URLs feature. This is most likely because Drupal doesn’t have permission to rewrite the path.

Fortunately, this is a very simple issue to correct. In order to gain access to the sites-available file for the site in question, it is necessary to log in via SSH or shell access on a Unix, Linux or a Mac. You can also access the shell by logging into the Rackspace cloud management console for your account. If you are trying to access SSH via a PC I recommend PuTTY.

All of the Rackspace Recipes on betaprogrammer.com reference the specific installation we’re currently using, which is Ubuntu server running apache2. Some differences may exist on other specific configurations.

Each site we publish on our Rackspace server uses a unique sites-available configuration file. These ‘files’ are located in a specific location by default: /etc/apache2/sites-available. Once logged into the server, type the following:

CD /etc/apache2/sites-available (enter)
sudo nano yoursitename (enter)

Once you’ve opened the specific site configuration file add the following:

<Directory /var/www/example>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-fsu
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</Directory>

Of course, change the directory from ‘example’ to the actual directory for the specific site in question. Once this is complete use control-x to exit nano and be sure to save the file under the same name. Once out of the site it couldn’t hurt to restart apache with the following command:

sudo /etc/init.d/apache2 reload

In order to determine success, simply log into the Drupal site and check to see if it is now possible to enable clean URL’s. (/admin/settings/clean-urls)

If you don’t have a lot of experience working in sites-available configuration files it is a good idea to start by saving a backup of the file contents. This way, if anything goes wrong, you can quickly revert back to the way it was before you started.

This technique can also be used to make it possible for a WordPress site to create simple paths. However, the code for WordPress is slightly different:

<Directory /var/www/example>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</Directory>
Related Posts Plugin for WordPress, Blogger...