您的位置:首页 > 运维架构 > Nginx

wordpress 在nginx服务器下重写路由,解决设置固定链接后出现404错误

2016-10-27 09:13 1081 查看
在利用wordpress搭建网站后,就想优化链接,这个没错,但是优化链接wordpress给出了几种固定链接的模式,选择固定链接后网站内页就出现了404错误。这个错误对于用过wordpress的你都遇到过,当然我也遇到过,但是遇到问题不怕,怕的时遇到后不理智的去解决。我在这里给大家一点我的解决方案。

首先出现这个404错误肯定是链接找不到了,那说明是路由问题,路由问题是由于设置了wordpress固定链接导致的结果,设置固定连接后服务器又没有配置路由规则,所以导致了404错误。

第二 原因找到后接着就是要确定你所使用的服务器是什么服务器,不同的服务器配置也不一样。

最后配置服务器路由规则,解决404问题,不同服务器具体解决方法如下:

一、Apache服务器

1、首先确定Apache是否加载了Mod_rewrite 模块

方法: 检查 /etc/httpd/conf/httpd.conf 中是否存在以下两段代码 (具体路径可能会有所不同,但形式基本是一样的):

a. LoadModule rewrite_module libexec/mod_rewrite.so

b. AddModule mod_rewrite.c

2、检查Apache是否开启.htaccess支持

vi  /etc/httpd/conf/httpd.conf

AllowOverride All   #如果后面参数为None需要修改为All(大概在338行)

3、在文件/etc/httpd/conf/httpd.conf相应的主机目录配置中加入如下代码(此时须注意,如果网站是通过虚拟主机来定义,请务必加到虚拟主机配置中去,否则可能无法使用。)

RewriteEngine On

RewriteBase /

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

如下:

vi /etc/httpd/conf/httpd.conf  #编辑

<Directory "/var/www/html">

Options Includes ExecCGI FollowSymLinks

AllowOverride All

Order allow,deny

Allow from all

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

</IfModule>

</Directory>

4、重启Apache

/etc/init.d/httpd restart

二、nginx服务器

1.找到/usr/local/nginx/conf/nginx.conf配置文件中的Server{}字段(注关与nginx配置文件详情讲解请查看文章《nginx.conf配置文件各个功能模块详细说明》)在Server字段中加入如下代码。

 

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;

}

我的Wordpress博客是的 server{}段是单独放到vhost/目录来存放每个网站的配置文件。最后再在nginx.conf配置文件下加载上对应的配置文件 include enjoykz.conf

貌似官方给出了新的开启wordpress固定链接的方法,也简单的多。这里假设,我在nginx的conf文件夹下创建个wordpress.conf ,将下面的代码粘贴进去:

location / {

try_files $uri $uri/ /index.php?$args;

}

最后再在你对应网站的配置文件中加载include wordpress.conf。

当你解决404页面问题后,发现后台又会出现404错误。有的人测底崩溃了,这里我也顺便给一个解决方案:就是在刚才添加的代码后面再添加一个规则:rewrite /wp-admin$ $scheme://$host$uri/ permanent;

最后保存文件:wq

重启nginx :server nginx restart

三、IIS服务器

在网站根目录新建一个文件保存为httpd.ini,在该文件下输入如下代码:

[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]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  wordpress
相关文章推荐