Join the community today
Become a Member

Wordpress Nginx, WordPress, and Trailing Slash 301 Redirects

Discussion in 'Blogs & CMS usage' started by deltahf, Mar 4, 2018.

  1. deltahf

    deltahf Premium Member Premium Member

    299
    134
    43
    Jun 8, 2014
    Ratings:
    +216
    Local Time:
    9:18 PM
    I've recently noted that Nginx is returning the same WordPress pages for multiple URLs, with and without the trailing slash. For example:

    www.example.com/contact

    and

    www.example.com/contact/

    ...will both return the same page, but they won't redirect to one form or the other. Even though using rel="canonical" can mitigate this issue, it can be an SEO concern and can cause mis-reporting in Google Analytics, because the address to the same page can have different URLs.

    Please correct me if I'm wrong, but I believe the Centminmod Nginx configuration, by default, will provide a 301 redirect to form of the URL with the trailing slash. So...

    www.example.com/contact

    => 301 redirect to... =>

    www.example.com/contact/

    That is how it works with actual directories, but if WordPress is handling the URL re-writes, it's not working as expected. I thought I had solved the issue with the following rule in my site's vhost.conf:

    Code:
    location ~ /^([^.\?]*[^/])$ {
            try_files       $uri @addslash;
    }
    location @addslash {
            return 301 $uri/;
    }
    
    This was tested and working for me, but it stopped and I don't know exactly when or why.

    Is there another way I should be doing this? Perhaps this is something that Centminmod's WordPress Nginx re-write rules can address in the future?
     
  2. eva2000

    eva2000 Administrator Staff Member

    37,296
    8,150
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +12,546
    Local Time:
    12:18 PM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    you shouldn't need any rule nginx defaults to redirecting to slash version of directory by default - what happens if you disable that custom redirect rule altogether ?

    i.e. on my wordpress7 blog

    Code (Text):
    http://wordpress7.centminmod.com/280/php-7-1-12-nginx-1-13-7-cloudflare-zlib-fork
    

    vs
    Code (Text):
    http://wordpress7.centminmod.com/280/php-7-1-12-nginx-1-13-7-cloudflare-zlib-fork/
    

    and
    Code (Text):
    http://wordpress7.centminmod.com/date/2017/12
    

    Code (Text):
    http://wordpress7.centminmod.com/date/2017/12/
    
     
    • Like Like x 1
..