25 Apr 2016

Generating SEO-Friendly URLs in Drupal

Submitted by Drupal Boss

Drupal does a lot of things well, but generating SEO-friendly URLs is not one of them. Every time you add new content to your Drupal website as a node, it generates a URL that looks something like 'http://www.yoursite.com/?q=node/29'. These kinds of URLs might be completely functional as far as your Drupal database is concerned, but search engines hate them. They want URLs that are somewhat descriptive of the content.

You can generate SEO-friendly URLs in Drupal by downloading and installing a number of modules that handle most of the work for you. However, we believe this is one instance in which doing it manually is better. The manual process is very simple and, once implemented, never has to be changed again. Using third-party extensions is always subject to those modules being updated.

Edit the .htaccess File

The first step is to edit the .htaccess file in your main Drupal directory. This file is used by web browsers to determine who has access to your site as a general user. If you were to make your site password-protected, it would involve this file as well.

To edit this file for our purposes here, first download a copy and save it to your hard drive – just in case anything goes wrong. Once saved, open it in a word processor, delete the entire contents, and add the following code:

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

If the relative path to your Drupal directory is anything other than the default directory specified by your host provider, you will need to edit the second line of the above code to reflect that difference. Save the file to a different directory (so you don't overwrite your downloaded copy) and then upload the new file to your main Drupal directory.

Configuring Your URLs

You are now ready to configure Drupal to automatically generate SEO-friendly URLs – provided the 'Path' module is already installed and activated on your site. If not, do that before moving on. You can install the module just as you do any other in module Drupal.

Once you're ready to go, navigate to your Drupal Dashboard and click on the 'Modules' tab along the top of the screen. A new page will appear. Scroll down the page until you find the 'Path' entry; check the box next to it and save the settings.

Now return to the menu at the top of your page and click 'Configuration'. Scroll down until you find 'Clean URLs'; click on it. You should find that the 'Enable clean URLs' option is already checked. If not, check it. Save the settings and you are done. If all is working correctly, you should be able to visit your site from the front end to check your links. Either hover your mouse over a few links or click directly on them to take you to the designated pages. You should have a nice, clean URLs that are based on file names.