Remove HTML extension for clean URLs using htaccess

0
982
Remove HTML extension for clean URLs using htaccess

To remove the .html extension from URLs using .htaccess in PHP, you can use URL rewriting. Here’s an example of how you can achieve this:

Create or modify the .htaccess file in the root directory of your PHP project.
Add the following code to the .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

This code will perform the following actions:

  • Enable the RewriteEngine.
  • Check if the requested URL is not a directory.
  • Check if the requested URL with the .html extension exists as a file.
  • Rewrite the URL internally, removing the .html extension.

Follow this video for complete guidance :

With this configuration, if someone requests a URL like example.com/page.html, the server will internally handle the request as example.com/page.

Make sure that your server has the mod_rewrite module enabled for this to work. Additionally, ensure that the file you want to access exists and has the .html extension.

ALSO READ  PHP and jQuery - Crop Image and Upload using Croppie plugin

Comments are closed.