How to remove both .php and .html extensions from url using NGINX?

How to remove both .php and .html extensions from url using NGINX?

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

Stefan Bogdanescu

Founder & Senior Architect

Cleaning URL Extensions in NGINX: Handling .php and .html Simultaneously

You want your NGINX server to serve clean URLs, stripping file extensions regardless of whether the underlying resource is a static HTML file or a PHP application. Your current configuration successfully handles the .html case but fails when you attempt to apply the same logic to .php. This mismatch stems from how NGINX serves static files versus how it proxies dynamic requests to PHP-FPM, and it requires distinct handling strategies for each content type.

The core issue lies in the try_files directive within your location / block. In your working configuration, you have:

location / {
    root   html;
    index  index.html index.htm index.php;
    try_files $uri.html $uri/ =404; 
}

This configuration works for .html files because NGINX serves static content directly from the root directory. When a user accesses indexhtml.html, try_files checks if that exact file exists. If not, it appends .html to the URI ($uri.html) and checks again. If that static file exists, NGINX serves it immediately without touching PHP-FPM.

However, this approach breaks for .php files. When you change try_files $uri.html to try_files $uri.php, you are telling NGINX to look for a static file named indexphp.php. If that file does not exist in your html directory, NGINX returns a 404 error. It never attempts to pass the request to PHP-FPM because the directive assumes the file is purely static.

To solve this, you must separate the logic for static assets and dynamic requests. You cannot use a single try_files chain to blindly guess between HTML and PHP extensions without risking infinite redirects or 404 errors. The standard pattern involves using specific location blocks to route based on the file extension or content type.

Handling Static HTML Files

For your HTML files, you can stick to the static serving model. You want indexhtml.html to redirect to indexhtml. Since try_files does not perform redirects by default but rather returns a 404 if the path isn't found, you need to ensure the canonical path (without extension) exists or force a redirect if the file with the extension is requested.

If your actual files are named indexhtml and indexphp inside your public directory, you should configure NGINX to strip the extension only if the cleaned version exists:

location / {
    # Allow requests without extensions first
    try_files $uri $uri/ @rewrite;
    
    # If no file is found, append .html and check again
    try_files $uri.html =404;
}

In this setup, if a user visits /indexhtml, NGINX checks for indexhtml. If it exists, it serves it. If not, it checks /indexhtml.html. This prevents double extensions like indexhtml.html from working unless the base file is missing, which might be intentional or require a 301 redirect logic depending on your SEO strategy.

Handling PHP Files

PHP files require a different approach because they are not static assets. They must be processed by the PHP-FPM service. You should create a specific location block for .php files to bypass the try_files static check entirely:

location ~ \.php$ {
    fastcgi_pass   php-fpm;
    fastcgi_index  index.php;
    include        fastcgi_params;
}

By isolating PHP requests, you ensure that indexphp.php is always passed to the PHP interpreter, regardless of whether a file named indexphp exists in your root. This prevents NGINX from looking for a static file when it should be executing code.

Managing Mixed Extensions

The trickiest part is handling the case where a user requests /indexhtml.html. If your canonical files are indexhtml (HTML) and indexphp (PHP), you need to decide if you want to serve indexhtml.html as an alias or reject it.

If you strictly want to remove extensions, you must ensure that the base filenames (indexhtml, indexphp) exist in your file system. If they do, the following configuration handles both gracefully:

  1. Requests for /path/to/file serve the static/dynamic file without extension.
  2. Requests for /path/to/file.html serve the HTML file if the base exists, or return a 404.
  3. Requests for /path/to/file.php are routed to PHP-FPM.

You can refine the location / block to handle the redirection logic explicitly if you want to normalize all requests to their extension-less forms:

location / {
    # Serve static files or pass to PHP if they exist without extension
    try_files $uri $uri/ @rewrite_ext;
}

location @rewrite_ext {
    # Append .html for HTML resources
    try_files $uri.html =403; 
}

However, a common pitfall is attempting to use try_files with multiple extensions in a single line like try_files $uri.html $uri.php. NGINX processes these conditions sequentially. If $uri.html doesn't exist, it checks the next one. But if neither exists and you don't have an explicit handler for PHP, you end up with a 404 instead of executing your script. Always prioritize the specific .php location block before applying generic try_files logic.

Finally, ensure your index directive lists files without extensions first or includes both to avoid ambiguity. If you are using Laravel or another framework that relies on .htaccess rules for routing, remember that NGINX does not process Apache directives unless explicitly configured to do so via the proxy_pass and include modules. For pure NGINX setups, rely on regex matches in location blocks to distinguish between static HTML and dynamic PHP content.

Our network