2

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.

1
  • 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 wordpress.stackexchange.com/questions/26388/… for example. Commented Sep 18, 2021 at 19:57

1 Answer 1

0

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.

4
  • 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: wordpress.org/plugins/redirection does exactly what I wanted to do. Commented Sep 17, 2021 at 22:01
  • 1
    SCRIPT_FILENAME should contain filename full with path, $document_root/index.php instead of /index.php for this case. Commented Sep 17, 2021 at 23:00
  • Oops! Fixed. Thanks @IvanShatsky Commented Sep 18, 2021 at 7:48
  • 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. Commented Sep 18, 2021 at 21:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.