How to remove .php or .html extensions from URL using .htaccess

Category: Web hosting

Remove .php extensions from URL

If you’re interested in having cleaner URLs on your site, you can configure your account to remove the .php or .html extensions. The result is more aesthetic URLs: for example, instead of https://www.example.com/demo.php, https://www.example.com/demo would be displayed. Some claim that it also helps SEO.

To achieve this, create a file .htaccess (including the dot, if it doesn’t already exist) directly in the public_html directory and add the following code:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]

With this code, if a visitor enters /demo, internally /demo.php is served but the URL is kept without the extension. However, if you go directly to /demo.php, you are not redirected to /demo. To also force this redirection add the following:

RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]

Remove .html extensions from URL

If you need to remove .html extensions, the code is very similar:

RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]

Ready! Now you can design your site with clean links:

<a href="https://example.com/mipagina" title="mipagina">mipagina</a>

Conclusion

The .htaccess file is very useful for cleaning up URLs and making them more user-friendly, but its syntax can be complicated. If you need it, there are online tools that help you generate it, for example htaccess editor .

Still need help?

If this guide didn’t solve your issue, our team can help you via ticket.