wordpress 在nginx伺服器下重寫路由,解決設定固定連結後出現404錯誤
在利用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]