安装好wordpress后默认文章固定链接后缀是 ?p=xx之类的,这样的链接虽然挺简洁,但对SEO不利。
在后台自定义固定链接后,打开网站除首页外,其他页面都是404。这是由于没有设置好Nginx对wordpress的rewrite规则,google了一些解决办法,说的都是要在Nginx的配置文件写入
server{
location / {
try_files $uri $uri/ /index.php?$args;
}
}
但是写上以上代码仍然不起作用。于是google到Nginx官网上对于wordpress的rewrite规则说明,照着例子改了自己的Nginx配置,参考如下
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9000;
}
server {
listen 80;
server_name www.zhenglinbo.com zhenglinbo.com;
root /var/www/blog;
index index.html index.htm index.php;
location / {
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;
}
error_page 404 /error/index.html;
location = /40x.html {
}
}
成功!使用自定义的文章固定链接为 /post-%post_id%.html 。
附Nginx官网对wordpress的rewrite规则说明:https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/