最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】WordPress更改固定链接后,导致文章帖子出现HTTP 404错误,无法显示,找不到网页,The webpage cannot be found

WordPress crifan 9010浏览 0评论

【背景】
首先,我们都已知道,wordpress默认设置的文章的链接地址为:
http://localhost/?p=9
其中该文章的9为postID。
而如果想要实现SEO,以及让文章链接地址更加具有可读性,最好的方式是,把上面的?p=9改为更加有意义的字符串
并且保证是英文,这样链接地址才可以更好的显示。
而其中把上面的地址,改为固定的某个地址,叫做固定链接。
而wordpress 3.3中,默认已经自带固定链接的功能了:
设置->固定链接,可以设置多种格式:
日期和名称型 http://localhost/2011/12/20/sample-post/
月份和名称型 http://localhost/2011/12/sample-post/
等等。
但是,去改了其中一种模式之后,再去打开之前的文章,却显示:
The webpage cannot be found
HTTP 404
即打不开网页,找不到网页了。
不过需要提及一点的是,我这里,虽然帖子无法打开,但是在主页中:
http://localhost/
却还是可以查看到那很多篇文章的,只是点击任何一篇,结果都是无法显示而已。
同样地,点击“← 早期文章”去打开第二页,看看更早的帖子,也都是打不开的。

【解决过程】
1.之前就遇到类似问题,最后是通过在固定链接设置中,在设置的模式之前,添加/index.php,而得以正常访问网页。
比如:
去把我所设置的固定链接的类型“日期和名称型“,其所对应的值为:
/%year%/%monthnum%/%day%/%postname%/
将其改为:
/index.php/%year%/%monthnum%/%day%/%postname%/
具体详情参考:
【已解决】wordpress中,设置为固定链接后,显示HTTP 404错误,文章无法打开 找不到网页

但是,此处好像也不起作用了,也无法解决问题。
而且也觉得,这个应该不是很好的方法,毕竟固定链接中还包含一个index.php,这算咋回事啊。。。
2.后来就到网上找,最后是从这里得到启发:
WordPress 修改固定链接后出现 Not Found 的解决方法
http://www.u-ce.com/topics/web-design.html
然后自己确保了:
(1)httpd.conf中
开启了对应的mode_rewrite模块:
把:
#LoadModule rewrite_module modules/mod_rewrite.so
改为:
LoadModule rewrite_module modules/mod_rewrite.so

(2)httpd.conf中把旧的:
/httpd-2.2-x64/htdocs
都改为了对应的当前的地址:
D:/tmp/WordPress/DevRoot/httpd-2.2.19-win64/httpd-2.2-x64/htdocs
(注:我这里的apache是解压的,所以地址没有自动改过来,如果你的apache是安装的,应该安装的时候,自动帮你改好的)

(3)htdocs根目录下已经存在了.htaccess,而且设置都已经正确。
并且,每次在wordpress中更改固定链接的配置时候,wordpress都会自动去更新这个.htaccess文件的。
所以,一般情况下,也不需要我们操心。
附上.htaccess的内容:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

(4)httpd.conf中,权限设置,是允许override权限的

httpd.conf中有多个关于AllowOverride的部分。

一个个简单解释一下:

A。全局的default的AllowOverride权限

#
# Each directory to which Apache has access can be configured with respect
# to which services and features are allowed and/or disabled in that
# directory (and its subdirectories). 
#
# First, we configure the "default" to be a very restrictive set of 
# features.  
#
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
</Directory>

很明显,英文注释解释了,处于安全考虑,此处是默认:

AllowOverride None

以及

Deny from all

关闭了所有的override的权限的和访问权限。

此处我们不需要理会。

B。htdocs部分的AllowOverride权限

#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
#
#
# This should be changed to whatever you set DocumentRoot to.
#
#<Directory "/httpd-2.2-x64/htdocs">
<Directory "D:/tmp/WordPress/DevRoot/httpd-2.2.19-win64/httpd-2.2-x64/htdocs">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.2/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    #AllowOverride None
    AllowOverride All
    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all
</Directory>

面默认是:

#AllowOverride None

我把其注释掉,改为我们所需要的:

AllowOverride All

即,此处需要允许所有的override。

这样,我们改了固定链接后,访问帖子文章,才有权限,才能正常访问。

(具体此变量的含义,至今不是彻底的十分清楚,只是知道此处起效而已)

C。其他部分的,好像是关于cgi的,目前不是很清楚,也不需要改。

【总结】

其他配置都已经正确的前提下,通过把httpd.conf中关于htdocs部分的AllowOverride None改为AllowOverride All

就可以实现固定链接修改后,文章仍旧可以正常访问了。

也就可以避免通过在固定链接地址模式添加/index.php的方式了。

【参考资料】

WordPress 修改固定链接后出现 Not Found 的解决方法

WordPress 修改固定链接后出现 Not Found 的解决方法

 

【后记】

后来又有一次,在本地wordpress中,遇到了此问题,在修改了固定链接后,结果帖子都打不开,出现404错误。

后来是通过去WAMP中开启Apache的mod_rewrite:

enable mod_rewrite

然后WAMP会自动重启Apache,然后就可以正常访问wordpress的帖子了。

看来只是默认的apache,没有开启mod_rewirte而导致此问题的。开启后,就解决了。

不需要改其他(默认的那些)设置的。

转载请注明:在路上 » 【已解决】WordPress更改固定链接后,导致文章帖子出现HTTP 404错误,无法显示,找不到网页,The webpage cannot be found

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (11)

  1. 最后一步需要重启apache服务service httpd restart,然后重新保存一下固定链接就行了。
    HumbleDust6年前 (2018-01-17)回复
  2. 有点不太明白呀
    个人博客8年前 (2016-01-02)回复
  3. 按照做了,rewrite和allowoverride都修改了,也还是一样的404,郁闷。
    玛丽全开9年前 (2015-08-17)回复
  4. 不错,解决了我的问题
    刘洋10年前 (2014-11-18)回复
  5. 问题解决了,谢谢! 唠叨一下:博主文章的链接实在是太长啦!
    篮联绽放12年前 (2012-10-26)回复
    • 的确是。个别帖子的固定链接,的确是很长。不过,一般来说,倒也不影响使用,哈哈。以后我会注意的,尽量不让固定链接太长了。
      crifan12年前 (2012-10-26)回复
90 queries in 0.172 seconds, using 22.20MB memory