How to hide extension in php using .htaccess ?

how to hide extension in php

PHP developers basically working on website development and the tasks given by client, but some-time when we are working on security based project then, we should aware about security.

Now a days hackers are very familiar with programming languages and it’s hacking tip and trick but first, they are trying to hack in which language the website is developer/built. So what we should do for keeping away our website for its language dependency. So without wasting any more time todays in this article, I will show you

How to hide extension in PHP?

So let assume that. We are working on a project which details is given below. I will show you directory wise so you can understand easily and quickly.

Project Name (Project Folder Name): My_Project

In the inside of My_Project I will create one index.php file which will automatically called when we run the project. So the directory will be looks like.

My_Project->index.php

Now What i want in this project is, I want to hide .php extension from the index.php, So Let’s do it step by step.

First create a new file in same/root directory (My_Project) and save as “.htaccess“.

Note: File name must be .htaccess

Now after successfully saved file, Write down below lines in that file Or You can copy and paste it directly in your .htaccess file.

RewriteEngine On
RewriteRule ^index/?$ index.php [NC,L]

Let’s understand What is meaning of the lines and why we should write this . Basically .htaccess file is used for given permission for specific task.

So Here we hide the extension from index.php [file.php] so it is called URL Rewriting in the Programming language. So In the first line, we are asking for On the RewriteEngine to rewrite the URL. and in the second line, first, we will put file name without extension (You can write any string or name) like below.

Ex. I want to rewrite index.php To home, So I will write as below

RewriteRule ^home/?$ index.php [NC,L]

And then after We will have to put original file name, You can put number of file in this .htaccess file.

I hope you guys enjoy. If you found this article very helpful Please share it and do comment for any question regarding .htaccess file.

Leave a Reply

Your email address will not be published. Required fields are marked *