項目遷移 WordPress 后僅首頁正常,其它頁面全部 404。
時隔一年,再度遇到這問題,總結和梳理一下。
1、想辦法登錄后台,刷新一次“設置”中的“固定鏈接”。
比如換成默認后保存,再設回原先設置並保存,理論上就可以解決絕大多數人的問題了。
2、檢查 Apache 的 rewrite mod 是否開啟。
LoadModule rewrite_module modules/mod_rewrite.so
3、檢查 Apache 的對應目錄 Allowoverride 是否設為 All。
(這次就在這里栽跟頭了,子目錄覆蓋了主目錄設置)
<Directory "/var/www/html/test"> AllowOverride All </Directory>
4、檢查 WordPress 主目錄下是否有正確設置的 .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
如果網頁服務器不是 Apache,而是 Nginx。
則需要在網站設置中增加以下代碼。
location / { try_files $uri $uri/ /index.php?$args; }
5、檢查 WordPress 整個目錄是否為 755 權限(文件 644 權限)。
(Window 系統下大概不用管后兩條)
find /path/to/wordpress/ -type d -exec chmod 755 {} \; find /path/to/wordpress/ -type f -exec chmod 644 {} \; chown -R apache:apache /path/to/wordpress/
修改所有者為 apache 以應對緩存/上傳/更新等問題。
用戶名因人而異,還可能是 www/daemon 等。
(還要注意檢查后台“多媒體-默認上傳路徑”)
6、如果配置的是虛擬主機那么記得在配置文件中加入開啟偽靜態的功能
<VirtualHost *:80>
DocumentRoot D:/www
ServerName localhost
RewriteEngine on
#規則放在下面即可
</VirtualHost>
可以對每個虛擬主機做單獨的URL Rewrite

