Join the community today
Register Now

Wordpress simple-301-redirects

Discussion in 'Blogs & CMS usage' started by jcat, Dec 8, 2015.

  1. jcat

    jcat Member

    53
    13
    8
    Jun 21, 2015
    Ratings:
    +20
    Local Time:
    11:13 AM
    Hello,

    We noticed an issue with this plugin: simple-301-redirects

    We kept seeing 404's being thrown for certain static files like .html, .htm, .pdf.

    By removing those extensions from /usr/local/nginx/conf/staticfiles.conf
    fixes the issue however the issue persists with .php

    If you use the plugin to redirect .php then it throws a 404

    The plugin uses the wp_redirect function so I think something in Nginx config is preventing WP from being able to use the function since Nginx acts before WP would.

    Any ideas?
     
  2. eva2000

    eva2000 Administrator Staff Member

    30,898
    6,908
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,403
    Local Time:
    2:13 AM
    Nginx 1.13.x
    MariaDB 5.5
    Never used that plugin so no idea myself. Maybe other folks would have some idea I'd check Nginx access and error logs and PHP error logs for clues. Do you have example of what full path urls you're trying to redirect from and to ? Default Apache and Nginx Wordpress rewrites usually pass everything through WP's index.php AFAIK.
     
  3. jcat

    jcat Member

    53
    13
    8
    Jun 21, 2015
    Ratings:
    +20
    Local Time:
    11:13 AM
    Gah, been at it for hours and hours, still no luck.

    I did a fresh OS and centmin install, installed a fresh WP and installed just this plugin.

    https://s3.amazonaws.com/uploads.hipchat.com/31137/205915/urAEwFXankAnoCz/upload.png

    /test works fine, redirects to the root of WP
    /test2 goes to the 404 page since there is no redirect
    /test.php goes to the default Nginx 404 page.


    Logging doesn't help at all, any thoughts at all :(
     
  4. eva2000

    eva2000 Administrator Staff Member

    30,898
    6,908
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,403
    Local Time:
    2:13 AM
    Nginx 1.13.x
    MariaDB 5.5
    AFAIK, that's due to essential security placed in /usr/local/nginx/conf/php.conf
    Code:
    try_files $uri =404
    for php best to do redirects at nginx level and not wp plugin if you DO NOT want your server compromised

    see why Setting up PHP-FastCGI and nginx? Don’t trust the tutorials: check your configuration! » Neal Poole
     
  5. eva2000

    eva2000 Administrator Staff Member

    30,898
    6,908
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,403
    Local Time:
    2:13 AM
    Nginx 1.13.x
    MariaDB 5.5
    Just do php redirects at nginx level Module ngx_http_rewrite_module & How To Create Temporary and Permanent Redirects with Apache and Nginx | DigitalOcean

    i.e.
    Code:
    location = /test.php {
            return 302 /;
    }
    Code:
    curl -I localhost/test.php
    HTTP/1.1 302 Moved Temporarily
    Server: nginx centminmod
    Date: Tue, 08 Dec 2015 06:08:45 GMT
    Content-Type: text/html
    Content-Length: 154
    Location: http://localhost/
    Connection: keep-alive
    Code:
    location = /test {
            return 302 /;
    }
    Code:
    curl -I localhost/test
    HTTP/1.1 302 Moved Temporarily
    Server: nginx centminmod
    Date: Tue, 08 Dec 2015 06:12:19 GMT
    Content-Type: text/html
    Content-Length: 154
    Location: http://localhost/
    Connection: keep-alive
    edit: actually from documentation probably better to have
    Code:
    location = /test.php {
            return 302 $scheme://$host/;
    }
    
    location ~ ^/test {
            return 302 $scheme://$host/;
    }
    
    as the /test directory needs to redirect for both /test and /test/

    Code:
    curl -I localhost/test/
    HTTP/1.1 302 Moved Temporarily
    Server: nginx centminmod
    Date: Tue, 08 Dec 2015 06:22:25 GMT
    Content-Type: text/html
    Content-Length: 154
    Connection: keep-alive
    Location: http://localhost/
    
    curl -I localhost/test
    HTTP/1.1 302 Moved Temporarily
    Server: nginx centminmod
    Date: Tue, 08 Dec 2015 06:22:27 GMT
    Content-Type: text/html
    Content-Length: 154
    Connection: keep-alive
    Location: http://localhost/
    
    performance wise, probably better if you did all those file redirects via nginx and not WP plugin
     
    Last edited: Dec 8, 2015
  6. jcat

    jcat Member

    53
    13
    8
    Jun 21, 2015
    Ratings:
    +20
    Local Time:
    11:13 AM
    Yeah trust me, if I could I would, there is about 300 redirects eh and a lot of the redirects contain spaces and ? I think the biggest obstacle is how to redirect the URL with ? like:

    /turning_65.php?keyword=medicare supplement leads

    I don't know how to escape the ? and I've googled for quite some time.
     
  7. eva2000

    eva2000 Administrator Staff Member

    30,898
    6,908
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,403
    Local Time:
    2:13 AM
    Nginx 1.13.x
    MariaDB 5.5
    if it's just old urls rewrite to web root index / , just redirect /turning_65.php to /
     
  8. eva2000

    eva2000 Administrator Staff Member

    30,898
    6,908
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,403
    Local Time:
    2:13 AM
    Nginx 1.13.x
    MariaDB 5.5
    Usually, Centmin Mod is provided as is so you're left to do rewrites, but was curious myself.

    See Converting rewrite rules and Converting Apache Rewrite Rules to NGINX Rewrite Rules and Pitfalls and Common Mistakes | NGINX for clues

    for /turning_65.php?keyword=medicare redirect to /
    Code:
    location ~ ^/turning_65.php {
            if ($arg_keyword ~ medicare) {
                    rewrite ^ / redirect;
            }
    }
    
    Code:
    curl -I localhost/turning_65.php?keyword=medicare
    HTTP/1.1 302 Moved Temporarily
    Server: nginx centminmod
    Date: Tue, 08 Dec 2015 07:27:45 GMT
    Content-Type: text/html
    Content-Length: 154
    Location: http://localhost/?keyword=medicare
    Connection: keep-alive
    
    for /turning_65.php?keyword=medicare supplement leads redirect to /

    browser shows
    Code:
    /?keyword=medicare%20supplement%20leads
    
    Code:
    location ~ ^/turning_65.php {
            if ($arg_keyword ~ "medicare%20supplement%20leads") {
                    rewrite ^ / redirect;
            }
    }
    
    Code:
    curl -I localhost/turning_65.php?keyword=medicare%20supplement%20leads
    HTTP/1.1 302 Moved Temporarily
    Server: nginx centminmod
    Date: Tue, 08 Dec 2015 07:27:01 GMT
    Content-Type: text/html
    Content-Length: 154
    Location: http://localhost/?keyword=medicare%20supplement%20leads
    Connection: keep-alive
    
    Or if you have alot of redirects use nginx map Module ngx_http_map_module

    in nginx.conf htttp{} context
    Code:
    map $arg_keyword $keyword_name {
        default 0;
        "~medicare%20supplement%20leads" 1;
        "~medicines" 1;
    }
    
    in site vhost
    Code:
    location ~ ^/turning_65.php {
        if ($keyword_name = 1) {
                    rewrite ^ / redirect; 
        }
    }
    
    Code:
    curl -I localhost/turning_65.php?keyword=medicare%20supplement%20leads
    HTTP/1.1 302 Moved Temporarily
    Server: nginx centminmod
    Date: Tue, 08 Dec 2015 07:36:27 GMT
    Content-Type: text/html
    Content-Length: 154
    Location: http://localhost/?keyword=medicare%20supplement%20leads
    Connection: keep-alive
    
    Code:
    curl -I localhost/turning_65.php?keyword=medicines
    HTTP/1.1 302 Moved Temporarily
    Server: nginx centminmod
    Date: Tue, 08 Dec 2015 07:36:36 GMT
    Content-Type: text/html
    Content-Length: 154
    Location: http://localhost/?keyword=medicines
    Connection: keep-alive
    
    or if you want to get rid of the ?keyword
    Code:
    location ~ ^/turning_65.php {
        if ($keyword_name = 1) {
            return 302 $scheme://$host/;
        }
    }
    
    Code:
    curl -I localhost/turning_65.php?keyword=medicines
    HTTP/1.1 302 Moved Temporarily
    Server: nginx centminmod
    Date: Tue, 08 Dec 2015 07:43:49 GMT
    Content-Type: text/html
    Content-Length: 154
    Connection: keep-alive
    Location: http://localhost/
    
    curl -I localhost/turning_65.php?keyword=medicare%20supplement%20leads
    HTTP/1.1 302 Moved Temporarily
    Server: nginx centminmod
    Date: Tue, 08 Dec 2015 07:43:57 GMT
    Content-Type: text/html
    Content-Length: 154
    Connection: keep-alive
    Location: http://localhost/
    nginx mapping and return 302 probably best. Use return 302 temp redirect until you are 100% sure they work and use curl -I for testing avoiding browser redirect caching issues or use browser private session incognito for testing. Once all okay change to return 301 permanent redirects
     
    Last edited: Dec 8, 2015