Feb 05, 2024

NGINX: Remove .html and .php Extensions from URL

Recently, I was migrating this blog from WordPress to static website, I encountered the challenge of preserving SEO-friendly URLs while generating separate HTML pages for each blog post. This post outlines how I successfully removed HTML and PHP extensions from the URLs using NGINX, ensuring a seamless transition without compromising link integrity.

Read Also: Migrating From WordPress to Static Site Generator

Removing HTML Extension

With each blog post now represented by a separate HTML page is having the same file name as the URL slug in WordPress. To remove HTML extension, I found a helpful solution in a stackoverflow thread.

location / {
    if ($request_uri ~ ^/(.*)\.html(\?|$)) {
        return 302 /$1$is_args$args;
    }
    try_files $uri $uri.html $uri/ =404;
}

This configuration allowed me to serve HTML pages without ".html" extension in the URL.

Removing HTML, Keeping PHP working

There were many custom PHP applications alongside WordPress. The static pages are generated for the blog. But there was need to update PHP applications also. It was time consuming and meanwhile I had to allow to run php as it was for custom applications.

Here is the NGINX configuration to continue processing PHP files while concealing the HTML extension.

 index index.html index.php;
location ~ \.php$ {
  try_files $uri =404;
  # add fastcgi_pass line here, depending if you use socket or port
  include fastcgi_params;
  fastcgi_pass php74;
}
location / {
    if ($request_uri ~ ^/(.*)(\.html|index)(\?|$)) {
       return 302 /$1$is_args$args;
    }
    try_files $uri $uri.html $uri/ =404;
  }

This configuration allowed me to seamlessly run PHP applications while ensuring that HTML pages remained hidden in the URLs.

Redirecting PHP to equivalent HTML Pages

For a smoother transition, I decided to update all PHP pages to their equivalent HTML counterparts, eliminating the need for PHP altogether. However, to maintain backward compatibility, I implemented a redirection mechanism to seamlessly direct PHP links to their corresponding HTML pages while concealing the extension.

index index.html;
location ~ \.php$ {
rewrite ^(.*)\.php$ $1 permanent;
}
location / {
if ($request_uri ~ ^/(.*)(\.html|index)(\?|$)) {
return 301 /$1$is_args$args;
}
try_files $uri $uri.html $uri/ =404;
}

This configuration ensures that URLs like techbrij.com/brijpad.php  seamlessly redirect to techbrij.com/brijpad displaying the content from brijpad.html page.

If you find yourself in a similar situation while migrating to a static site or dealing with NGINX configurations, I hope these solutions prove helpful. Maintaining consistent and clean URLs is crucial for SEO, and with the right NGINX setup, you can achieve this seamlessly.