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

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

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.

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>This is How to remove .html and .php Extensions</title>
</head>
<body>
<h1>Hello, It`s a test.php page | <a href="test">click here</a> to remove .php and look at the url in addressbar</h1>
</body>
</html>

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.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

 

2 thoughts on “How to Remove .php or .html extension”

  1. David Spector

    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.

Leave a Comment

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

Scroll to Top