How to remove .php .html extensions using .htaccess
Before understanding .htaccess code for url manupulation to remove file extensions such as .php .html, Lets understand What is .htaccess file?
A .htaccess file is a simple ASCII file. Which we can create using Notepad or TextEditor. Remember one thing that .htaccess file is far different from .html or .php file, .htaccess file is not user redable file. Syntax wich we use to define .htaacess file is followed down in code.
Basically, .htaccess file is used to lets server know what configuration changes to make on a per-directory basis.
Features
.htaccess can perform:
- 1) Redirect the user to different page
- 2) Rewrite URLs
- 3) Handle Error Documents
- 4) Block users by IP Address
In this article we'll going focus on rewriting URLs.
Lets start by creating three files:
- 1. .htaccess - for our url manupulation
In .htaccess, we going to remove file extension such as .php .html or anyother extension. To remove .php & .html extension from a PHP or HTML file for example mysite.com/demo.php to yoursite.com/demo you have to add the following code inside the .htaccess file:
Lets! go through the code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Same above code used to remove any file extensions from url just remove file extension name as shown below and another file extension name
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Now URL is ready to use to see result, Remember one thing that after manupulating url in .htaccess dont mention file name with extension just right file name server will automatically detect file location through .htaccess . Below is the example to write file name in <a>
tag
<a href="https://yoursite.com/demo">demo</a>
Hope! above article will clear your concept about .htaccess if you have issues then comment it down will surely go through that & provide solution as soon as possible