我有一个设置为Post名称的WordPress站点。
使用Nginx重写,我正在尝试内部重定向(浏览器URL没有改变),但到目前为止,我失败了。
This工作,但URL更改了
location ~ ^/u/(.*) {
rewrite ^/u/(.*) /p/?username=$1 redirect;
}I不明白为什么这不起作用:
location ~ ^/u/(.*) {
# this returns 404
rewrite ^/u/(.*) /p/?username=$1 last;
}The全局配置
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;
}
}我正在编写一个WordPress插件,我希望用户输入https://example.com/u/john71之类的URL,并在内部重定向到https://example.com/p/?username=john71
u和p都是WordPress页面,p包含允许我检索用户名的短代码。
我可以使用https://example.com/p/?username=john71,但是类似于:https://example.com/u/john71看起来更好。
欢迎对重写规则或方法的任何帮助。
发布于 2021-09-17 07:07:50
URI最终被重写到/index.php,因此WordPress从未看到额外的内部重写。
WordPress解析原始请求,该请求在REQUEST_URI参数中传递给它(该参数将在fastcgi-php.conf文件中定义,其中可能包括来自另一个名为fastcgi_params的文件)。
您可以通过在调用REQUEST_URI之前直接设置fastcgi_pass和SCRIPT_FILENAME来完成这项工作--例如:
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;
}我还没有包括fastcgi-php.conf,因为它可能包含一个在这个location中不能工作的try_files语句。
https://serverfault.com/questions/1077842
复制相似问题