Web Development
Remove .php and .html Extensions via .htaccess
· Linet M.
Removing .php or .html from a URL turns example.com/index.php into example.com/index. Apache's mod_rewrite handles this in a few lines inside .htaccess. The result: cleaner URLs that are easier to share and easier for users to type.
What is an .htaccess file?

.htaccess is an Apache server configuration file placed in a directory. It controls that directory's behaviour without touching the main server config. Key facts:
- It is a plain text file with no filename, only an extension:
.htaccess, notfile.htaccess. - It works only on servers running Apache HTTP Server.
- Settings in
.htaccessapply to the directory it sits in, plus every subdirectory beneath it.
What .htaccess can control
.htaccess handles a range of server behaviours:
- MIME type definitions
- Custom error pages (404, 500, etc.)
- Password protection on directories
- Blocking specific IP addresses or bot user-agents
- Preventing directory listing
- Blocking hotlinked images
- Redirecting URLs
- Changing the default directory index page
Removing the .php or .html extension
Place .htaccess in the root of your site (or the subdirectory you want to affect), then add the block below. The comment on line 4 shows how to adapt it for .html files.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
</IfModule>
To strip .html instead, replace every .php reference with .html:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]
</IfModule>
What each directive does:
RewriteEngine onactivates mod_rewrite for this directory.RewriteCond %{REQUEST_FILENAME} !-dskips the rule when the request matches a real directory.RewriteCond %{REQUEST_FILENAME}.php -fproceeds only when a.phpfile with that name actually exists on disk.RewriteRule ^(.*)$ $1.php [L]rewrites the bare URL internally to the.phpfile, without changing what the browser sees. The[L]flag stops further rule processing.
Testing the rewrite
Create a file called test.php in the same directory as .htaccess, with this content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Extension removal test</title>
</head>
<body>
<h1>test.php loaded via <a href="test">example.com/test</a></h1>
</body>
</html>
Visit example.com/test in a browser (no .php). If the page loads, the rewrite works. The anchor text in the page uses the bare URL, so clicking it confirms round-trip behaviour.
Adding a trailing slash
To enforce URLs like example.com/index/ instead of example.com/index, use the block below. Swap .php for .html if your files use the HTML extension.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
The [R=301,L] on the last rule issues a permanent redirect, which tells search engines and browsers to update their records to the slash-terminated form.
Related reading
If you work with PHP regularly, 13 PHP Tips That Make Development Faster covers error handling, PDO, and other practical patterns. 7 Popular PHP Frameworks Compared breaks down Laravel, Symfony, and five others by architecture and use case.
For broader web programming help, see Do My Programming Homework.
Related articles
- PHP
7 Popular PHP Frameworks Compared
Compare 7 PHP frameworks (Laravel, CodeIgniter, CakePHP, Zend, Yii, Symfony, Phalcon) on architecture, performance, and best use cases.
Aug 26, 2016
- Featured
Programming Project Ideas to Build and Earn
10 programming project ideas for students and developers: web apps, social networks, computer vision, robotics, and open source contribution.
Oct 15, 2014
- Featured
PHP Object-Oriented Programming for Beginners
Learn PHP OOP from scratch: classes, objects, magic methods, inheritance, and visibility modifiers explained with working code examples.
Sep 14, 2014


