Score:1

wordpress internal redirect using nginx

mx flag

I have a WordPress site with permalinks set to Post name.

Using Nginx rewrite, I am trying to have an internal redirect (with no change in browser URL), but so far, I am failing.

This works, but the URL changes

location ~ ^/u/(.*) {
    rewrite ^/u/(.*) /p/?username=$1 redirect; 
}

I don't understand why this does not work:

location ~ ^/u/(.*) {
    # this returns 404
    rewrite ^/u/(.*) /p/?username=$1 last;
}

The whole config

server {
    listen 80;
    listen [::]:80;
    server_name example.com;

    root /srv/www/html;
    error_log /var/log/nginx/error.log;
    index index.php;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~ ^/u/(.*) {
        rewrite ^/u/(.*) /p/?username=$1 last;
        # try_files $uri /p/?username=$1;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        client_max_body_size 8M;
        include fastcgi-php.conf;

        # Mitigate https://httpoxy.org/ vulnerabilities
        fastcgi_param HTTP_PROXY "";
        fastcgi_intercept_errors on;
        fastcgi_pass 0.0.0.0:9000;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }
}

I am writing a WordPress plugin, and I want users to enter URLs such as https://example.com/u/john71 and internally redirect to https://example.com/p/?username=john71

Both u and p are WordPress pages, and p contains a shortcode that allows me to retrieve the username.

I could have https://example.com/p/?username=john71, but something like: https://example.com/u/john71 looks better.

Any help on the rewrite rule or approach is welcome.

us flag
You should do the custom request routing part in WordPress completely. If you split the logic between nginx and WordPress, you are forcing your users to add extra configuration. Look at https://wordpress.stackexchange.com/questions/26388/how-to-create-custom-url-routes for example.
Score:0
jp flag

The URI is ultimately rewritten to /index.php - so the extra internal rewrite is never seen by WordPress.

WordPress parses the original request, which is passed to it in the REQUEST_URI parameter (which will be defined in your fastcgi-php.conf file, which probably includes it from another file called fastcgi_params).

You may be able to make this work by setting REQUEST_URI and SCRIPT_FILENAME directly before calling fastcgi_pass - for example:

location ~ ^/u/(.*) {
    include fastcgi_params;

    fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    fastcgi_param REQUEST_URI /p/?username=$1;

    fastcgi_param HTTP_PROXY "";
    fastcgi_intercept_errors on;
    fastcgi_pass 0.0.0.0:9000;
}

I haven't included fastcgi-php.conf as it may contain a try_files statement that will not work in this location.

mx flag
Thank you, Richard. It's a good idea, but I could not make it work; I keep getting Not Found (404). I found this plugin: https://wordpress.org/plugins/redirection/ does exactly what I wanted to do.
Ivan Shatsky avatar
gr flag
`SCRIPT_FILENAME` should contain filename full with path, `$document_root/index.php` instead of `/index.php` for this case.
Richard Smith avatar
jp flag
Oops! Fixed. Thanks @IvanShatsky
mx flag
I tried with `$document_root/index.php`, but also I was getting 404. It worked when I replaced `/p/?username=$1` with `p=2154` (2154 is the id of the page). I don't know enough about WordPress internal routing. The `redirection` plugin works well so I am settling with that for now.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.