Enabling Clean URL


Enabling Clean URL


What is a Clean URL

With the use of Dynamic website web know that the content created is not stored as single pages. The pages/content we create are stored in a back-end database.  

We someone requests for a specific content the core code (of CMS of dynamic website) queries the databases and fetched different parts of the page and assembles these together and shows to the user.

Usually this set of associated content of a page (title, main content, images, tags etc) are assigned with a unique id for the CRUD operations.

Most of the request of these pages are sent as part of the URL with the pages id.

Many a times the URL looks like the following

https://opensourcecook.in/index.php?page=blog&id=123

This type of URL does not convey any meaning to either the user of is useful for search engine,  instead of these if we have something like

https://www.opensourcecook.in/blog/post/iot/what-smart-device 

The above URL clearly indicated that the it is a "blog post" with main topic "IoT" and with discussion about "What are smart Device".

Characteristics of a Clean URL:

  • Readable: Uses meaningful words instead of random characters or numbers.
  • Short & Simple: Avoids unnecessary parameters, making it user-friendly.
  • SEO-friendly: Helps search engines understand the page content better.
  • No Special Characters: Avoids symbols like ?, &, =, and $ when possible.
  • Consistent Structure: Follows a logical hierarchy, like /products/shoes/sparks

Drupal/WordPress and Clean URL

When we install Drupal/WordPress it by default uses the clean URL.  If the clean URL is not set in our web-server (Apache) then you will find it difficult to work with Drupal or many pages will show error.

To enable clean URL in "Apache" do the following:

Check Available Apache Modules

To check if `mod_rewrite` is available or needs to be installed in Apache, follow these steps:

  • Method 1: Check Available Apache Modules
    • Run the following command in your terminal: (login as root or use sudo)
      • apachectl -M | grep rewrite
    • OR (for systems using `httpd` instead of `apachectl`):
      • httpd -M | grep rewrite
    • If you see `rewrite_module (shared)`, it means `mod_rewrite` is already enabled.
    • If no output appears, `mod_rewrite` is either disabled or not installed.
  • Method 2: Check via PHP Info
    • If you have PHP running, you can check via `phpinfo()`:
      • Create a new PHP file in your web root (e.g., `/var/www/html/info.php`): (i.e. the document root)
        • sudo nano /var/www/html/info.php
      • Add the following code:
        • <?php echo phpinfo(); ?>
      • Save and exit, then open `http://yourserver/info.php` in a browser.  
      • Look for `mod_rewrite` under the "Loaded Modules" section.
  • If you can see mod_rewrite available you can skip the installation steps and directly enable it
  • How to Install and Enable mod_rewrite (If Needed)
    • For Debian/Ubuntu/Mint
    • sudo apt update
    • sudo apt install apache2
  • For CentOS/RHEL:
    • yum install httpd
    • systemctl restart httpd
  • To enable mod_rewrite
    • sudo a2enmod rewrite
    • sudo systemctl restart apache2
 

To enable `mod_rewrite` in Apache, follow these steps:

  • Make sure the module is enabled
    • Enable mod_rewrite**
    • Run the following command in the terminal to enable the `mod_rewrite` module:
      • sudo a2enmod rewrite
  • Configure Apache to Allow Overrides
    • Modify your Apache configuration to allow `.htaccess` files to use `mod_rewrite`.  
    • For Ubuntu/Debian:
      • Edit the default Apache configuration file:
        • sudo nano /etc/apache2/apache2.conf
      • Find the following section:
        • <Directory /var/www/>
          Options Indexes FollowSymLinks
          AllowOverride None
          Require all granted
          </Directory>
      • Change `AllowOverride None`  to `AllowOverride All`:
        • <Directory /var/www/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
          </Directory>
    • For CentOS/RHEL:
      • Edit the virtual host configuration file, typically located in:
      • nano /etc/httpd/conf/httpd.conf
      • Find the `<Directory>` section for your website and change `AllowOverride None` to `AllowOverride All`.
    • Restart Apache
      • After making the changes, restart Apache to apply them:
      • sudo systemctl restart apache2  # For Ubuntu/Debian
      • sudo systemctl restart httpd    # For CentOS/RHEL
    • Test mod_rewrite
      • Create a `.htaccess` file in your website’s root directory (e.g., `/var/www/html/.htaccess`) with the following content:
      • RewriteEngine On
        RewriteRule ^test$ test.html [L]
      • Then, try accessing `http://yourdomain.com/test`.
        • If it loads `test.html`, `mod_rewrite` is working correctly.
Main category
Address

OpenSourceCook.in
"Natraj"  Bungalow,
Colony No.7,  Sr.No. 38.
(Lane Behind Sai Baba Mandir)
Kale Borate Nagar, Hadapsar,
Pune - 411028.
Get Directions