Learn about Centmin Mod LEMP Stack today
Become a Member

Wordpress How to have conditional redirect for nginx?

Discussion in 'Blogs & CMS usage' started by Chuong Luong, Oct 27, 2020.

  1. Chuong Luong

    Chuong Luong Member

    31
    0
    6
    Aug 8, 2019
    Ratings:
    +2
    Local Time:
    8:40 AM
    Hi,

    I have a situation like this:

    I have a redirect in nginx:

    Code:
    location /old-url.html {
       rewrite ^/.* http://$server_name/new-url.html permanent;
    }
    My purpose is for people from outside of my site, go to old-url, they will then be redirected to new-url. But when they are in my website and after doing some tests, they click the link to the old-url

    So, within the new-url page, I have a "click here" link which linked to old-url, therefore they can go to the exact old-url.html not new-url.html anymore.

    But with the redirect in nginx above, I make the visitor go to only new-url.html, like a loop.

    Can this be possible to do with nginx? If, yes, how can I do? I am using Wordpress (with Centminmod + Cache Enabler) but I don't want to touch too much in the code or plugins of my wordpress site.

    Thanks a lot!
     
    Last edited: Oct 28, 2020
  2. Chuong Luong

    Chuong Luong Member

    31
    0
    6
    Aug 8, 2019
    Ratings:
    +2
    Local Time:
    8:40 AM
    After a while of searching, I am trying this:

    Code:
    location /old-article.html {
        if ($http_referer !~ (www\.)?mydomain\.com) {
        return 301 http://$server_name/new-article.html;
        }
    }
    When accessing the old-article while browsing mydomain.com, it returned 404. Dont' know what I did wrong.
     
  3. Chuong Luong

    Chuong Luong Member

    31
    0
    6
    Aug 8, 2019
    Ratings:
    +2
    Local Time:
    8:40 AM
    Just an update, I found a solution, with this config:

    Code:
    location /old.html {
            root /home/nginx/domains/mydomain.com/public;
            try_files $uri $uri/ /index.php?$args;
            if ($http_referer !~ (www\.)?mydomain\.com){
            return 301 https://mydomain.com/new.html;
            }
    }
     
  4. eva2000

    eva2000 Administrator Staff Member

    45,652
    10,358
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +16,076
    Local Time:
    11:40 AM
    Nginx 1.19.x
    MariaDB 5.5/10.x
    interesting approach. Thanks for sharing your final solution :)
     
  5. Chuong Luong

    Chuong Luong Member

    31
    0
    6
    Aug 8, 2019
    Ratings:
    +2
    Local Time:
    8:40 AM
    I am still find way to work around the 301 redirect browser cache, I need to force no cache for redirection ... :( my solution only works for the first visit, from the second visit, the loop happens.
     
  6. eva2000

    eva2000 Administrator Staff Member

    45,652
    10,358
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +16,076
    Local Time:
    11:40 AM
    Nginx 1.19.x
    MariaDB 5.5/10.x
    don't use 301 permanent browser cached redirect but use 302 temp redirects
     
  7. Chuong Luong

    Chuong Luong Member

    31
    0
    6
    Aug 8, 2019
    Ratings:
    +2
    Local Time:
    8:40 AM
    Because the old post has good backlinks, so I don't want to use 302 to get rid of those good links. That's why I use 301. Is there anyway to tell the browser to not cache on the new post url?
     
  8. eva2000

    eva2000 Administrator Staff Member

    45,652
    10,358
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +16,076
    Local Time:
    11:40 AM
    Nginx 1.19.x
    MariaDB 5.5/10.x
    302 and 301 are treated the same by Google and search engines, you don't loose any SEO juice.

    see Google: SEOs Are Wrong, 302 Redirects Pass PageRank

     
  9. Chuong Luong

    Chuong Luong Member

    31
    0
    6
    Aug 8, 2019
    Ratings:
    +2
    Local Time:
    8:40 AM
    Thanks for your info. From Google point of view, 302 & 301 don't affect your ranks. On the other hand, People still said that they can maintain/get back their ranks better with 301.

    https://www.searchenginejournal.com/301-vs-302-redirects-seo/299843/

    Also, I also look at a bigger scene, like Bing or Yahoo, I read somewhere that they treat 2 of them differently. That's why for my safe bet, I chose the 301 :D