【发布时间】:2012-02-16 13:39:46
【问题描述】:
将我的 wordpress 永久链接结构从 /%category%/%postname%/ 重定向到 /%postname%/ 的 nginx 重写规则是什么?
【问题讨论】:
标签: wordpress url-rewriting nginx seo
将我的 wordpress 永久链接结构从 /%category%/%postname%/ 重定向到 /%postname%/ 的 nginx 重写规则是什么?
【问题讨论】:
标签: wordpress url-rewriting nginx seo
总之,你需要让 NGINX 知道如果该文件不存在,不要抛出 404 错误,而是调用index.php。 Wordpress 足够聪明,可以将 URL 解析为参数,并提供正确的页面。
在你的服务器配置块中添加这个 sn-p:
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;
}
}
【讨论】:
try_files $uri $uri/ =404;用/index.php?$args 替换=404 是让我的类别链接再次工作的必要步骤。