Link to home
Start Free TrialLog in
Avatar of Pete Long
Pete LongFlag for United Kingdom of Great Britain and Northern Ireland

asked on

NGINX and Redirects

Hi,

I've spent a few weeks messing around with nginx to get to grips with it before I migrate my Wordpress site, (currently on Apache).

One thing can't work out is, 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

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

In Apache this did the trick
-=-=-=-=-=-=-=-=-
RewriteEngine On
RewriteBase /
RewriteRule ^KB/Article/(.*).htm  http://www.mysite.com/KB/Article/$1 [L]
-=-=-=-=-=-=-=-=-

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
Avatar of Nicholas
Nicholas

Try this before your main location block

location /KB {
rewrite ^/KB/Article/(.*).htm http://www.mysite.com/KB/Article/$1 redirect;
}

Open in new window

SOLUTION
Avatar of Pete Long
Pete Long
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Pete Long

ASKER

Thanks for the following up, I've been offline for a while - trying now

P
Same Result :( stops me getting to the main page also when I add that, I ge the welcome to nginx page instead.

Pete
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yep thats the one!

:)
Found the solution myself