将我的 wordpress 永久链接结构从 /%category%/%postname%/
重定向到 /%postname%/
的 nginx 重写规则是什么?
最佳答案
总而言之,您需要让 NGINX 知道如果该文件不存在,不要抛出 404 错误,而是调用 index.php
。 Wordpress 足够聪明,可以将 URL 解析为参数,并提供正确的页面。
在您的服务器配置 block 中添加此代码段:
location / {
try_files $uri $uri/ /index.php?$args;
}
这是来自 nginx.org 的完整示例:
# Upstream to abstract backend connection(s) for php
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9000;
}
server {
## Your website name goes here.
server_name domain.tld;
## Your only path reference.
root /var/www/wordpress;
## This should be in your http block and if it is, it's not needed here.
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 / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
关于wordpress - 用于获取干净 URL 的 NGINX 重写规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8967682/