I have a WordPress website where my URLs are currently written as follows
http://example.com/for-sale/12345-aaa-123-street-bbb-city/
and I would like to still serve the same URL, but I want the URL to look like
http://example.com/city/street/
I have created a rewrite rule of rewrite for-sale/([0-9]+)-aaa-(.*)-bbb-(.*) /$3/$2/ permanent;
This does rewrite my URL but returns a 404 error. Here is what the rewrite log returns
2021/11/04 04:48:39 [notice] 23077#23077: *4643 rewritten redirect: "/Vernon//539-Apache-County-Road-3144/", client: 108.162.215.21, server: example.com, request: "GET /for-sale/20071-aaa-539-Apache-County-Road-3144-bbb-Vernon/ HTTP/1.1", host: "example.com", referrer: "https://example.com/vernon/"
2021/11/04 04:48:40 [notice] 23077#23077: *4646 "for-sale/([0-9]+)-aaa-(.*)-bbb-(.*)" matches "/for-sale/14808-aaa-35640A-Hwy-60-bbb-Vernon/", client: 172.70.98.47, server: example.com, request: "GET /for-sale/14808-aaa-35640A-Hwy-60-bbb-Vernon/ HTTP/1.1", host: "example.com", referrer: "https://example.com/vernon/"
2021/11/04 04:48:40 [notice] 23077#23077: *4646 rewritten redirect: "/Vernon//35640A-Hwy-60/", client: 172.70.98.47, server: example.com, request: "GET /for-sale/14808-aaa-35640A-Hwy-60-bbb-Vernon/ HTTP/1.1", host: "example.com", referrer: "https://example.com/vernon/"
2021/11/04 04:48:41 [notice] 23077#23077: *4649 "for-sale/([0-9]+)-aaa-(.*)-bbb-(.*)" does not match "/for-sale/19617-2926-DEER-HILL-Road-Heber-Arizona-85928/", client: 108.162.245.49, server: example.com, request: "GET /for-sale/19617-2926-DEER-HILL-Road-Heber-Arizona-85928/ HTTP/1.1", host: "example.com"
2021/11/04 04:48:41 [notice] 23077#23077: *4651 "for-sale/([0-9]+)-aaa-(.*)-bbb-(.*)" matches "/for-sale/6944-aaa-35640-Hwy-60-bbb-Vernon/", client: 172.69.35.180, server: example.com, request: "GET /for-sale/6944-aaa-35640-Hwy-60-bbb-Vernon/ HTTP/1.1", host: "example.com", referrer: "https://example.com/vernon/"
2021/11/04 04:48:41 [notice] 23077#23077: *4651 rewritten redirect: "/Vernon//35640-Hwy-60/", client: 172.69.35.180, server: example.com, request: "GET /for-sale/6944-aaa-35640-Hwy-60-bbb-Vernon/ HTTP/1.1", host: "example.com", referrer: "https://example.com/vernon/"
2021/11/04 04:48:42 [notice] 23077#23077: *4654 "for-sale/([0-9]+)-aaa-(.*)-bbb-(.*)" matches "/for-sale/18232-aaa-72-County-Road-8144-bbb-Vernon/", client: 172.172.35.168, server: example.com, request: "GET /for-sale/18232-aaa-72-County-Road-8144-bbb-Vernon/ HTTP/1.1", host: "example.com", referrer: "https://example.com/vernon/"
2021/11/04 04:48:42 [notice] 23077#23077: *4654 rewritten redirect: "/Vernon//72-County-Road-8144/", client: 172.172.35.168, server: example.com, request: "GET /for-sale/18232-aaa-72-County-Road-8144-bbb-Vernon/ HTTP/1.1", host: "example.com", referrer: "https://example.com/vernon/"
I'm not sure if I'm headed down the right path here, or is a rewrite rule wrong to be using in this scenario? I've tried placing the rewrite rule in it's own location, and tried different flags but this is the closest I have got so far.
Here is my full config. Thanks for any help.
server {
server_name example.com www.example.com;
access_log /var/www/example.com/logs/access.log;
error_log /var/www/example.com/logs/error.log;
rewrite_log on;
root /var/www/example.com/public;
index index.php;
listen 443 ssl http2; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location / {
rewrite for-sale/([0-9]+)-aaa-(.*)-bbb-(.*) /$3/$2/ permanent;
try_files $uri $uri/ /index.php?$args;
}
# fastcgi
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
fastcgi_read_timeout 150;
}
#Cache static files for as long as possible
location ~*.(ogg|ogv|svg|svgz|eot|otf|woff|woff2|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz)$ { expires 365d;
}
location ~ ^/(wp-admin|wp-login|ngx_pagespeed_statistics|ngx_pagespeed_global_statistics|ngx_pagespeed_message)$ { #auth_basic "Private";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
}
location ~* /wp-includes/.*.php$ {
deny all;
access_log off;
log_not_found off;
}
location ~* /wp-content/.*.php$ {
deny all;
access_log off;
log_not_found off;
}
}
server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name example.com www.example.com;
listen 80;
return 404; # managed by Certbot
}