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.