解决nginx下WordPress伪静态只能打开首页

如果您的网站没有进行固定链接设置的话,即使nginx没有作伪静态设置,也不会出现只能打开首页,不能打开内页的情况。 也就是在后台的设置->固定链接,将固定链接设置为默认的形式,内页的地址是以”?P=”的形式,这一点曾作过测试,不过,一般稍微懂点优化的站长,估计都对链接地址已经作了伪静态处理,如下图:

nginx下WordPress伪静态设置

好了,一起来看看,具体的操作过程。

1.首先我们通过FTP软件下载vi /usr/local/nginx/conf/nginx.conf这个路径下的nginx配置文件,找到server_name localhost;在后面一行添加上面的代码:

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;
}
}
然后保存,覆盖原文件.

2.重启nginx

直接在putty等远程连接软件上,输入以下命令即可。

service nginx restart

再去试试内页,是否是已经打得开了。当然,如果是新站的话,可以首先将这个设置好。

WordPress伪静态打开内页出现404提示的解决

如果您按上面的方法操作后,还提示404页面,那么就很有可能是设置有问题,也就是说,伪静态规则没有生效。

我们可以将上面的代码,加到:/usr/local/nginx/conf/vhost/www.mfbuluo.org.conf这个文件中。当然,您的文件应该是以您的域名来命名的。

在下面的代码中加入:

listen 80;
server_name www.mfbuluo.com;
index index.html index.htm index.php default.html default.htm default.php;
成功加入后如下:

listen 80;
server_name www.mfbuluo.com;
index index.html index.htm index.php default.html default.htm default.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;
}
然后再重启您的nginx,输入下面的命令:

service nginx restart

这里的话,就不要去修改/usr/local/nginx/conf/nginx.conf 更多详细:传送门

附录:伪静态规则全 - IIS伪静态规则

IIS 环境是 Windows 主机常用的服务器环境,新建一个 txt 文件,将下面的代码添加到文件中:

[ISAPI_Rewrite]

Defend your computer from some worm attacks

#RewriteRule .(?:global.asa|default.ida|root.exe|..). . [F,I,O]

3600 = 1 hour

CacheClockRate 3600 RepeatLimit 32

Protect httpd.ini and httpd.parse.errors files

from accessing through HTTP

Rules to ensure that normal content gets through

RewriteRule /tag/(.) /index.php\?tag=$1 RewriteRule /software-files/(.) /software-files/$1 [L] RewriteRule /images/(.*) /images/$1 [L] RewriteRule /sitemap.xml /sitemap.xml [L] RewriteRule /favicon.ico /favicon.ico [L]

For file-based wordpress content (i.e. theme), admin, etc.

RewriteRule /wp-(.*) /wp-$1 [L]

For normal wordpress content, via index.php

RewriteRule ^/$ /index.php [L] RewriteRule /(.*) /index.php/$1 [L]

然后另存为 httpd.ini 文件,上传到WordPress站点的根目录即可。

Apache伪静态规则

Apache是 Linux 主机下常见的环境,现在一般的 Linux 虚拟主机都采用这种环境。新建一个 htaccess.txt 文件,添加下面的代码:

RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
然后上传到 WordPress 站点的根目录,重命名为 .htaccess 即可

Nginx伪静态规则

Nginx环境一般是Linux 主机 VPS或服务器用户用的比较多,这些用户一般都会自己配置Nginx,或者有专门的人帮你配置,打开 nginx.conf 或者某个站点的配置环境,比如 wpdaxue.com.conf(不同人配置的不一样),在 server { } 大括号里面添加下面的代码:

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;
}
}
保存,重启 Nginx 即可。

Comments: 0

「人生在世,留句话给我吧」

提交评论