How to enable clean URLs in Drupal?

Member

by vinnie , in category: SEO , 15 days ago

How to enable clean URLs in Drupal?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by naomi_cronin , 14 days ago

@vinnie 

Enabling clean URLs in Drupal is an important step in improving the usability and SEO of your site. Here’s how you can enable it:


For Drupal 7:

  1. Check Apache Configuration: Ensure that the mod_rewrite module is enabled in your Apache configuration. You can enable it by running: a2enmod rewrite Restart Apache to apply the changes: service apache2 restart
  2. Configure .htaccess File: Make sure the .htaccess file is present in the root directory of your Drupal installation. This file usually comes with the default Drupal installation.
  3. Check Clean URL Configuration in Drupal: Log in to your Drupal admin interface. Navigate to Configuration > Search and metadata > Clean URLs. Run the test by clicking the link provided to check clean URL compatibility. If your server is configured properly, you should be able to enable clean URLs by checking the option.


For Drupal 8 and Above:

  1. Check Apache Configuration: Similar to Drupal 7, ensure that mod_rewrite is enabled.
  2. Verify .htaccess File: Ensure the .htaccess file exists in your Drupal root directory.
  3. Enable Clean URLs: Unlike Drupal 7, clean URLs are enabled by default in Drupal 8 and above. If you’re experiencing issues, it’s typically a server configuration problem rather than a Drupal configuration.


For Nginx:


If you're using Nginx instead of Apache, you’ll need to configure your server block to handle clean URLs. Here’s a basic example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
server {
    listen 80;
    server_name yourdomain.com;

    root /path/to/drupal;
    index index.php index.html index.htm;

    location / {
        try_files $uri /index.php?$query_string;
    }

    location ~ '.php$|^/update.php' {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # or whatever php version you're using
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~* .(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        try_files $uri @rewrite;
        expires max;
        log_not_found off;
    }

    location @rewrite {
        rewrite ^/(.*)$ /index.php?q=$1;
    }
}


Make sure to replace /path/to/drupal with your actual Drupal installation path and yourdomain.com with your actual domain.


Testing Clean URLs:To verify that clean URLs are working, try accessing a page on your Drupal site with and without the ?q= parameter. If both URLs lead to the same page (without redirection), clean URLs are functioning correctly.


Ensure that your server configurations match the requirements for the Drupal version you’re using and contact your hosting provider if you encounter persistent issues.