• I’m trying to get custom rewrite rules working under Nginx.
    Currently the setup works perfectly in Apache with .htaccess

    What I have is a Page where I want all requests to go when a pattern is matched.
    The Page’s permalink is http://example.com/article/
    The matched pattern is http://example.com/article/{slug-match}

    Following the doc here https://codex.wordpress.org/Rewrite_API I’ve got a simple rewrite function setup in my theme’s functions.php

    function custom_rewrite_rule() {
        add_rewrite_rule('^article/([^/]*)/','index.php/article/','top');
    }
    add_action('init', 'custom_rewrite_rule');

    Which generated the following .htaccess file (I know, this has no impact on Nginx, it’s just info since this IS working in apache)

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteRule ^^article/([^/]*)/? /index.php/article [QSA,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress

    Now in Nginx I’ve updated the location directive in the config file to catch that

    location / {
            rewrite ^/article/(.+) /index.php/article last;
            try_files $uri $uri/ /index.php$query_string;
        }

    And the Nginx logs look like that’s firing correctly

    2016/06/16 02:56:52 [notice] 12879#12879: *1 “^/article/(.+)” matches “/article/test-article”, client: 192.168.10.1, server: example.com, request: “GET /article/test-article HTTP/1.1”, host: “example.com”
    2016/06/16 02:56:52 [notice] 12879#12879: *1 rewritten data: “/index.php/article/”, args: “”, client: 192.168.10.1, server: example.com, request: “GET /article/test-article HTTP/1.1”, host: “example.com”

    On top of that if I got to http://example.com/index.php/article as the rewrite suggests, I end up on the correct page.

    I’ve flushed the rewrite rules, both programmatically and using the “Save” on the permalink page.

    So the big question is, is there something I’m missing on the WordPress side, a configuration that needs to be applied to Nginx not applied to Apache.

    I’m really stumped. Any help is appreciated.

    Thanks,
    Eldon

  • The topic ‘Nginx Rewrite for non multi-site config’ is closed to new replies.