我有一个 WordPress 网站,其中我的 URL 目前写如下
http://example.com/for-sale/12345-aaa-123-street-bbb-city/
我仍然希望提供相同的 URL,但我希望 URL 看起来像
http://example.com/city/street/
我已经创建了重写规则rewrite for-sale/([0-9]+)-aaa-(.*)-bbb-(.*) /$3/$2/ permanent;
这确实重写了我的 URL,但返回了 404 错误。以下是重写日志返回的内容
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/"
我不确定我是否走对了路,或者在这种情况下重写规则是否不合适?我尝试将重写规则放在它自己的位置,并尝试了不同的标志,但这是我目前最接近的。
这是我的完整配置。感谢您的帮助。
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
}
答案1
这不是一个答案,但它太长了,不能作为评论,所以我把它写成一个答案。
你想要实现的目标是可能的。但是要实现它,你需要一个更复杂的软件堆栈。你需要一些键值存储(例如Redis),以及能够在处理请求时使用它的 Web 服务器(例如,带有lua-nginx-模块和lua-resty-redis)。
这是(其中一种)可能的算法。
- 收到请求后,使用请求 URI 作为键检查存储是否包含某些值。
- 如果是,则从存储中获取该值并将其用作 WordPress 的 URI。
- 如果不是,请检查 URI 是否与我们的正则表达式模式匹配。
- 如果是,请执行以下操作:
- 生成重写的 URI;
- 将原始 URI 和重写的 URI 作为键/值存储在存储器中;
- 返回带有新 URI 的重定向。
- 如果不是,则以通常的方式提供我们的请求 URI。
- 如果是,请执行以下操作:
实现此堆栈的完整方法肯定超出了常规 ServerFault 答案的范围,但您可以将其视为一个想法。