Drupal 7 Clean URLs

It been 12 hours of searching and test, all of the hint’s have been tried more then one.
And finally it works!!
And its so simple ;-).

After installing Drupal 7.x i start to test, one off the things i need to use is Clean URLs. First lets see if it works out of the box, Admin>Configuration>Clean URLs. No it has failed!
The host i am on is an shared host, the main server configuration is out off reach.

There some problems
* Where is the .htaccess?
* What should the content of .htaccess be placed?
* Is there sample?

Let start with the Drupal installation, the site is installed into www.example.com/drupal. No changes have jet been made.

Fist stop is the page the give me http://drupal.org/node/15365
Things to check
* mod_rewrite is enabled
* .htaccess is working

Check mod_rewrite. Report>Status report>PHP (click in the link beside the php version number), search for mod_rewrite. Yes its loaded.

Check if .htaccess is working
Lets see where is the .htaccess file? Hmm i can’t find it. Lets create one in the www root with the next line as its content.

RedirectMatch 301 /drupal(.*) http://www.example.com

While trying to open the url http://www.example.com/drupal i am redirected to http://www.example.com like it should do.
Now that we know its working lets remove the line from .htaccess en go to the site. But what is this? I can’t go to the site, the redirect rule is still working. How can that be the .htaccess is empty?
This problem had me going for a while but its the browser that tricks us here. Just empty the cache the redirect rule is gone. So remember to empty the browsers cache every time you change the .htaccess.

All i need to do now is fill the .htaccess with the correct rules and put it in the correct place.
Let the game begin.

What rules do i need to use?
After some search it looks like this is what i need

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

Until now i have always put the .htaccess in the www root, lets put it there now. Don’t forget to empty the browsers cache. Now run the Clean URLs test, it fails again.
Ok lets move it .htaccess to the drupal directory and try again. No not again it fails

After reading many post i noticed that some people are talking over stripping the default Drupal .htaccess. What is the content of this file and why cant i find it on my site?
Why is it not on my site? I think this has to do with the installation, i just skip this. Its not there then i have to get it some where.
What’s the content of the default .htaccess that comes with Drupal?
To find this out i download the tar.gz/zip and extract it. If you are on an system that hides dot files you need to make them visible to see .htaccess. Before i do anything else i just read the content, until i know what to do.

  # Modify the RewriteBase if you are using Drupal in a subdirectory or in a
  # VirtualDocumentRoot and the rewrite rules are not working properly.
  # For example if your site is at http://example.com/drupal uncomment and
  # modify the following line:
  # RewriteBase /drupal
  #
  # If your site is running in a VirtualDocumentRoot at http://example.com/,
  # uncomment the following line:
  # RewriteBase /

in the sample i use the site is in /drupal, all i have to do is uncomment the next line.

  # RewriteBase /drupal

If you site is in /prd_my_site then use

  # RewriteBase /prd_my_site

Now lets try this file. First i put it in the root. Now i rerun the Clean URLs test, hmm it fails, but remember where i start reading (http://drupal.org/node/15365), there is a section called Clean-Urls Test – False Negatives. The test can tell you its not working, but it is working. So lets try.
This is the default way

http://www.example.com/drupal/?q=user

Now try this

http://www.example.com/drupal/user

For me it worked!
Let’s do an other test, copy the original .htaccess (unchanged) to the /drupal directory and remove or rename the one in the www root. Lets test the url again, well ok it works. Great now i can have more then one installation in one domain.

But we are not yet ready, we need to tell Drupal that we want to use the Clean URLs. The problem is that the test will fail, we need to do it an other way. Lets to to this url

http://www.example.com/drupal/admin/config/search/clean-urls

you now will see a checkbox to enable the Clean URLs and a button to save the configuration. One you update the setting and saved it your done!

Enjoy the Clean URLs

Leave a Reply