Remove .php extension from URL using htaccess

Hi friends, You might wonder seeing some sites will not display any .php extension even though they developed the website using normal PHP script.

This can be achieved using htaccess. It’s damn simple and here is the code:

The below script will change your URL from:

From: https://www.yoursite.com/mysite.php
to: https://www.yoursite.com/mysite

Save the below script as (.htaccess) and upload it in your root folder or the folder you keep all your files (also work in sub-folder too!)

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>

NOTE: Better download notepad++ to save this file as .htaccess, because default notepad won’t allow it!

For your convenience I put a demo here you can check in both the ways:

with extension:
https://theonlytutorials.com/demo/myfile.php

without extension:
https://theonlytutorials.com/demo/myfile

I had put this code in the sub-folder (demo) of my site!

For your better convenience, I have given the Download too!

Download

5 thoughts on “Remove .php extension from URL using htaccess

  1. I created .htaccess file and also uploaded on my root folder, when i run the file it gives error “Internal Server Error

    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Please contact the server administrator, admin@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

    More information about this error may be available in the server error log.”

Leave a Reply

Theme: Overlay by Kaira
Agurchand