How to hide .php extension in .htaccess

How to hide .php extension in .htaccess

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Hide .php Extensions with .htaccess

When a developer requests a URL like folder/name, the server often defaults to serving folder/name.php. While this works for legacy applications, modern design systems and SEO best practices frequently require hiding the extension entirely. If you are attempting to redirect folder/name to folder/name.php using .htaccess, your initial rule might look like this:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule ^folder/([a-zA-Z_\-0-9]+)/?$ /folder/$1.php [L]
</IfModule>

This specific snippet is likely failing because of how Apache handles rewrite rules relative to existing files. The regular expression ^folder/([a-zA-Z_\-0-9]+)/?$ matches any string that could be a filename, but it doesn't account for the fact that if a file named folder/file.txt exists, or even folder/file.php, the rule might interfere with existing paths. More critically, this rule attempts to map requests to files that technically exist in a different location (with an extension), which can trigger internal redirects that Apache may block if AllowOverride is restricted or if the directory structure isn't set up to handle the rewrite chain correctly.

The most robust solution involves ensuring the target file exists and that the rewrite rule explicitly targets only requests without extensions. You must also ensure you are not accidentally rewriting requests for files that already have an extension. A refined approach uses a negative lookahead or simply relies on the fact that RewriteRule processes rules in order. However, the simplest and most effective pattern often looks like this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    # Redirect requests without .php to the version with .php
    RewriteRule ^folder/([^\.]+)$ /folder/$1.php [L,QSA]
</IfModule>

This rule captures any string in folder/ that does not start with a dot (preventing it from matching hidden files or other extensions) and appends .php. The [QSA] flag is crucial here; it preserves the original query string. Without it, parameters like ?id=5 would be lost during the redirect.

Handling Existing Files

A common pitfall occurs when you try to hide extensions for URLs that point to existing files or directories. If a user visits folder/style.css, you generally do not want Apache to rewrite this to folder/style.css.php. The rule above handles this because the regex [^\.]+ stops matching at the first dot. If your URL is folder/style.css, the engine sees the dot and does not apply the rule.

However, if you are dealing with a complex directory structure where some subfolders also contain .php files, you must ensure the rewrite logic doesn't conflict with them. The regex pattern needs to be specific enough to match only the intended clean URLs. If your folder names vary (e.g., blog, shop, about), hardcoding ^folder/ is limiting. A more dynamic rule would be:

RewriteRule ^([^\.]+)/?$ /$1.php [L,QSA]

This captures any first-level directory name and appends .php. This assumes your file structure matches the URL structure exactly.

Security and Performance Considerations

Before deploying this configuration, verify that mod_rewrite is enabled on your server. You can check this with apachectl -M | grep rewrite on Linux or by inspecting your hosting control panel. If the module is missing, none of these rules will take effect.

Another critical consideration is the order of operations in .htaccess. Rules should be placed near the top, after ensuring no other conflicting redirects exist. Additionally, check your server's AllowOverride directive in the main Apache configuration file (httpd.conf or apache2.conf). If AllowOverride All is not set for your document root, your .htaccess file will be ignored entirely.

Alternative Approaches

If you are using a framework like Laravel, modifying .htaccess to hide extensions is often unnecessary because the router handles clean URLs by default. In such cases, relying on the framework's internal routing mechanisms is safer than server-level rewrites. However, for traditional PHP applications or shared hosting environments where you have direct access to .htaccess, the method described above remains the industry standard.

For more advanced scenarios involving multiple extensions (like .html or .htm) alongside .php, you might need a separate rule block:

RewriteRule ^([^\.]+)/?$ /$1.php [L,QSA]
RewriteRule ^([^\.]+)\.html$ /$1.php [L,QSA]
RewriteRule ^([^\.]+)\.htm$ /$1.php [L,QSA]

While this adds complexity, it ensures consistency across different file types if your application supports them. Always test these rules in a staging environment first. A single misplaced rule can break access to existing assets or create infinite loops if the target file does not exist and returns a 404 instead of being found by the server.

Our network