How to Remove .php or .html extension

In this short tutorial you’ll learn how to remove extensions like .php and .html using the .htaccess file. It has been seen that most of the developers wants to remove extension from the website in order to make the URLs more user & SEO friendly. This feature can be implement to do the same on your website or in your projects using the .htaccess file.

Recently, I was working on my personal website wanted to remove the extensions of the URL of the web, in order to make URLs is easier to use. In this tutorial you will see how to do that easily, by using .htaccess file.

Let’s take a look at how to remove .php and .html extensions with the help of .htaccess file. Let’s begin from the definition of .htaccess file.

What is an .htaccess file?

remove php and html extensions using htaccess.htaccess file has various definition to describe clearly:

  • An .htaccess file is an ASCII file written with any text editor such as Notepad, Notepad++ or using any editor.
  • .htaccess is the simply file’s extension; it isn’t file.htaccess.
  • .htaccess file is configuration file used on web servers running on Apache server.
  • .htaccess  file is used for enable or disable the certain features provided by Apache.
  • .htaccess file provides a way to change configurations settings in a directory.

Features of .htaccess File

.htaccess file set many features which is useful for the web developers. Below are some of the features provided along with .htaccess file

  • MIME types
  • Preventing hot links of images and any other file types
  • Preventing directory listing
  • Providing Error Documents file
  • Providing Password protection
  • Restricting Spam visitors and Block them by using IP Address
  • Blocking bad bots and spam boats
  • Changing default directory page
  • Redirects URLs

Removing .php or .html Extension

To remove the .php or .html extension from your site follow the steps below

STEP.1 DO IT

For ex. you need to change example.com/index.php to example.com/index  Now, to implement this, you must put the following code in the .htaccess file. So create a new file with any text editor and save it as .htaccess. The following code is for the .php extension. The same process is for html, simply changing the line of code where we refer to the extension, as in the previous case it was .php, and this should be .html just replace .php with .html if yow want to remove .html extension

STEP.2 TEST IT

We create a new file called test.php and save it to the root folder where already saved .htaccess file. Now you can access the url without having to put the file extension .php.

How to add a trailing slash at the end of URL

Now let’s add a slash at the end of the url like this: http://example.com/index/ . Link to the HTML or PHP file as shown above. Just to change the code if you want it applied to an HTML file.

 

2 comments

Here’s the way I do it. This solution is simpler (rewrite rules cannot be debugged other than by testing the overall functioning), and uses only Core directives for speed as compared with Rewrite rules:

# Serve no-extension files using PHP:

SetHandler application/x-httpd-php

PS – This is tested.

Thanks for the comment.
I will definitely update the article very soon

Leave a reply