Yogesh Chauhan's Blog

How to Remove PHP File Extensions From Your Website URLs?

in PHP on November 13, 2019

Look at the following URLs. Which one looks a bit better?

https://yogeshchauhan.com/project.php

https://yogeshchauhan.com/project

The second one looks a bit letter as the first one doesn't look like human friendly URL. Because not everyone understands what project.php is but almost everyone understands what project is.

To remove any file's extension we need to make changes in our .htaccess file. Check your website folder on your server. There must be one file without name but with .htaccess as the file type. If you don't have it on your server folder, make a new file in notepad and save it as ".htaccess". Yes. Without a name but with an extension. If you have the file on the server then just download it and make changes into the same file and then after the changes are done, upload it to your server folder. 

Let's take a look at the code to remove the file extension in PHP.

Removing the file extension

If you want to completely remove the file extension use the following code. JUST ADD THE FOLLOWING CODE TO YOUR .htaccess FILE AND SAVE IT.

Make sure you change the domain name to your domain name otherwise it won't work.


RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /.*index HTTP/
RewriteRule ^(.*)index$ http://your-domain-name.com/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://your-domain-name.com/$1 [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /(.+).php HTTP/
RewriteRule ^(.+).php$ http://your-domain-name.com/$1 [L,R=301]
RewriteRule ^([a-z]+)$ /$1.php [L]

The example above will remove the extension .php from your website URLs. So, for example project.php will appear as project.

Now your job is not done yet. Once you upload the .htaccess file to your server, you'll notice that some of your URLs will appear without the .php extension but the browser will display 404 error in some cases. It doesn't happen with all URLs but with few URLs only.

The error occurs because the server doesn't allow file to load without an extension. WEIRD!

If you are facing some problems like 404 error after adding the code above then add the following code as well. 

So, we need to add few more lines of code to the same .htaccess file.


Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]

The code above will force server to load any file without any extension and you'll be good to go ahead. 


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to create bouncing balls using HTML canvas and JavaScript?HTMLUse SwiperJS to create mobile touch sliders fastMiscellaneousSolution for Xcode 11 Command PhaseScriptExecution failed with a nonzero exit code errorMiscellaneousWordPress: How to get ACF field values from another post?WordPressIs Software Really Eating The World? I don’t think soMiscellaneousImplicit and Explicit Joins in Oracle SQLSQL/MySQL