WordPress更改固定连接(permalinks)后只能访问首页问题处理

今天整理了一下网站,想将固定链接调整一下,但是又出现了只能访问首页,其他子页面提示无法访问的问题,联想到之前也出现过同样的问题,然后这次就研究了一下。

看到了wordpress官网关于固定链接的介绍,发现需要开启apache的mod_rewrite module才能正常使用Pretty permalinks,

以下是原文:

Using mod_rewrite or lighttpd you can produce much nicer permalinks (see Pretty Permalinks). There are many different formats, but the most common, and most versatile looks like

http://example.com/2012/post-name/

or

http://example.com/2012/12/30/post-name

Pretty permalinks are available under:

  • Apache web server with the mod_rewrite module
  • Nginx using try-files, e.g. according to this tutorial
  • Hiawatha web server with UrlToolkit support enabled.
  • Lighttpd using a 404 handler or mod_rewrite
  • Caddy using rewrite, e.g. according to this tutorial

随后找了一下开启的方法,在这里找到了

如果不想打开链接的话,直接往下看即可。

我直接把对应的部分拷贝了出来,如果想看中文的话可以拉到最后

Enable Mod_rewrite Module

The mod_rewrite module is enabled by default on CentOS 7. If you find it is not enabled on your server, you can enable it by editing 00-base.conf file located in /etc/httpd/conf.modules.d/ directory.

sudo nano /etc/httpd/conf.modules.d/00-base.conf

Add or uncomment the following line:

LoadModule rewrite_module modules/mod_rewrite.so

Save and close the file, then restart the httpd service:

sudo systemctl restart httpd

Enable .Htaccess File

Once the mod_rewrite module has been activated, you can set up your URL rewrites by creating an .htaccess file in your default document root directory. A .htaccess file allows us to modify our rewrite rules without accessing server configuration files. For this reason, .htaccess is critical to your web server. Before we begin, we need to allow Apache to read .htaccess files located under the /var/www/html directory.

You can do this by editing httpd.conf file:

sudo nano /etc/httpd/conf/httpd.conf

Find the section <directory /var/www/html> and change AllowOverride None to AllowOverride All

 <Directory /var/www/html>
    AllowOverride All
 </Directory>

Save and exit.

Now restart Apache to put the change into effect:

sudo systemctl restart httpd

第一步大概意思就是修改/etc/httpd/conf.modules.d/00-base.conf文件,将

LoadModule rewrite_module modules/mod_rewrite.so

这一句前的注释取消,如果没有这一句的话直接加上即可

我看了一下,我的是这个文件里的这部分是正常的,就没有调整。

然后我按照第二步调整了一下,网站显示就正常了,下面是调整步骤

修改/etc/httpd/conf/httpd.conf文件,找到如下类型的文本,修改为

 <Directory /var/www/html>
    AllowOverride All
 </Directory>

修改完以后重启apache即可

sudo systemctl restart httpd

文本修改的方法可以参考我的关于vim配置的文章

此处理方案仅是个人环境的处理方案,仅供参考,有问题的话也可以留言讨论

Comments Closed.