Fixing Clean URLs in Drupal 7

I am in the middle of installing Drupal 7 on a server at my workplace. I ran into a curious problem with Drupal Clean URLs – namely, I couldn’t get them working. Here’s my breakdown of the problem and how I resolved it.

Troubleshoot: Drupal Clean URLS (Drupal 7)

Problem:

Home » Administration » Configuration » Search and metadata » Clean URLs

There is an option to run the Clean URL test. The test should either come back with a 404 error, or the page saying that the test was successful and you can now set your option checkbox. Unfortunately, when clicking the button all the page did was reload. No other messages were seen.

Solution:

Obviously, if the server can’t support clean URLs then I should at least get a 404. A better result would be an error message giving me hints. (This bug has been confirmed in Drupal 7 and the fix is still in process).

The way I fixed it was following these steps on my Apache Server (Ubuntu 10):

(Note: I am assuming here that you are running a dedicated server and have access to the Apache process. If not, contact your system administrator)

1. Make sure the rewrite module is enabled by going to /etc/apache2/mods-enabled. If “rewrite.load” is not there you will have to add it:

$ sudo a2enmod rewrite

2. Add this code to /etc/apache2/httpd.conf:

# fix for clean urls
<Directory /var/www/yoursite>
AllowOverride all
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</Directory>

Then save the file.

Note: if your site is located in a spot other than “/var/www/yoursite” you’ll have to use that path instead.

Note: if your site isn’t located at the root, you should include the directory name. For example, if your site is accessed on the web as “http://yoursite.com/blog/” then you’ll probably need to write in the path “/var/www/yoursite/blog”. If you do this, you’ll also need to change the line that says “RewriteBase” to: “RewriteBase /blog/”

So, in this case, your addition to httpd.conf will look like this:

# fix for clean urls
<Directory /var/www/yoursite/blog>
AllowOverride all
RewriteEngine on
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</Directory>

3. Restart Apache

$ sudo /etc/init.d/apache2 restart

4. Finally, rerun your Clean URL test. You should now see a checkbox for enabling URLs. (You may have to clear your Drupal cache and Browser cache before running the test).

-Bill

One comment

Comments are closed.