Welcome! Log In Create A New Profile

Advanced

Rewrite to remove htm extension

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

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

http://www.mysite.com/KB/Article/000001.htm
to
http://www.mysite.com/KB/Article/000001

(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
/etc/nginx/sites-enabled/default
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

server_name 123.123.123.123;

# 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

Pete
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;

Pete
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: 152
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready