本网站的结构是Linux+Nginx+Mysql+php。
wp的固定链接默认是朴素模式 : https://www.dinganan.cn/?p=123

写过几篇文章后发现链接地址好难看,一眼看不出文章的标题,而且非常不利于SEO, 所以我将默认的固定链接形式 改成文章名: https://www.dinganan.cn/sample-post/ 也就是 https://www.dinganan.cn /%postname%/
保存更改后,网站首页还可以访问,但是其他的页面全都是404错误。

以下是解决方法,适用于 Nginx网页服务器
方法一:try_files
编辑Nginx的配置 文件,在”location /”这一部分当中添加下面的一行文字:
try_files $uri $uri/ /index.php;
具体配置如下:
location / {
root /var/www/html;
index index.html index.php;
try_files $uri $uri/ /index.php;
}
然后重启Nginx(centos6:service nginx restart;centos7:systemctl restart nginx)。如果还会出现404问题则需重启php-fpm(centos6:service php-fpm restart;centos7:systemctl restart php-fpm)。
不过,不知道为什么文章的url是正常了,wp的文章编辑器又不正常。

所以我采用了下面另一种方法:
方法二:rewrite
编辑Nginx的配置 文件,在”location /”这一部分当中添加下面几行判断并重写的语句:
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
并在location外添加重写语句:
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
具体配置如下:
location / {
root /var/www/html;
index index.html index.php;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break; }
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php; }
if (!-f $request_filename){
rewrite (.*) /index.php; }
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
最后重启Nginx(和上面的方法一样)。
最新评论