Menu Close

Remove the .html or .php from the URL or link of the Website

Codeamend

Use the following .htaccess code to quickly remove the URL (www.url.com/contact.html) or the php URL (www.url.com/contact.php) from your website or URL.

Here’s an example of what your website might look like when it opens:

“www.url.com/contact.html”  or  “www.url.com/contact.php” 

Please add the following code to the root .htaccess file of your site if you wish to remove the .html & .php extensions from the URLs and websites:

For HTML Websites

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

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R=301,L,NE] 

For PHP Websites

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

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L,NE] 

The result of adding your website will be as follows: (example)

“www.url.com/contact” 

It will also help with redirects if someone opens (“(www.url.com/contact.html” ) by force, it will reroute to ( “(www.url.com/contact” his will prevent duplicate website content issues.

NOTE : It is recommended that you back up your existing .htaccess file before adding these rules.

You have to follow these steps:

  • Log in to cPanel account.
  • In the Files section, click on the File Manager icon.
  • Click on the Settings Button in the top right corner.
  • If you want to make changes in the Primary Domain then Click on the radio button next to the Web Root. If changes are to be made on Other Domains, then Click the dropdown menu and find the domain in which changes are to be made.
  • Remember to check the checkbox next to Show Hidden Files. Now click the Save Button to return to the File Manager window.
  • Now you are in the Root Folder of the domain which you have selected to make changes. Search for the .htaccess file and right-click on it. Click on the Edit option in the menu. You can now add code to the .htaccess file.
  • Add the above code inside the .htaccess file
  • Click on the Save Changes Button and then on Close Button.

You can now link any page inside the HTML document without needing to add the extension of the page as no extension will be visible now in the URL of the website.

Posted in htaccess, Web Technologies

You can also read...