Welcome! Log In Create A New Profile


Rewrite to remove htm extension

Posted by PeteLong 
Rewrite to remove htm extension
May 30, 2017 03:36PM

Hopefully this is a simple one.

I'm about to migrate form Apache to nginx, Ive got everything setup ok, apart form one thing;

In Apache I have a rewrite (in .htaccess) that redirects any request for


(So it strips the .htm off the end)

It's because the site was originally written in flat html and theres thousands of links going to the old URLs.

How Do I do the same thing in nginx? I'm assuming I add the redirect to my
File, as that the one the test site is using;

# Default server configuration

server {
listen 80 default_server;
listen [::]:80 default_server;

# Set The Root Directory for the Entire Website

root /var/www/html/;

# Add index.php to the list if you are using PHP

index index.html index.htm index.nginx-debian.html;

# Add The Server IP Address or FQDN


# The following does the WorkPress Rewrites for the permalinks

location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;


# pass PHP scripts to FastCGI server

location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include snippets/fastcgi-php.conf;

A) What do I need to add to replicate that Apache/.htaccess redirect.
B) Is there anything you would recommend I add/remove from the above config file.

Thanks in advance

Re: Rewrite to remove htm extension
May 31, 2017 10:08AM
Never mind I've worked it out

Put this in the server block

rewrite ^(/.*)\.htm(\?.*)?$ $1$2 permanent;

Re: Rewrite to remove htm extension
August 02, 2017 05:04AM
Instead of redirect you can use:

location /KB/Article {
if (!-e $request_filename) {
rewrite ^/KB/Article/(.*)$ /KB/Article/$1.html last;
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 82
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 254 on July 05, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready