不少朋友總是詢問 WordPress 如何添加偽靜態(tài)規(guī)則,今天倡萌就總結(jié)一下 IIS/Apache/Nginx 三種環(huán)境下的偽靜態(tài)規(guī)則,希望對(duì)大家有所幫助。
檢測(cè)主機(jī)是否支持偽靜態(tài)的方法:在WP后臺(tái) > 設(shè)置 > 固定鏈接,設(shè)置為 非默認(rèn)帶?的那種結(jié)構(gòu),然后訪問任何一篇文章,如果出現(xiàn) 404 錯(cuò)誤,說明你的主機(jī)當(dāng)前不支持 WordPress 偽靜態(tài)。

IIS偽靜態(tài)規(guī)則
IIS 環(huán)境是 Windows 主機(jī)常用的服務(wù)器環(huán)境,新建一個(gè) txt 文件,將下面的代碼添加到文件中:
[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]
然后另存為 httpd.ini 文件,上傳到WordPress站點(diǎn)的根目錄即可。
Apache偽靜態(tài)規(guī)則
Apache是 Linux 主機(jī)下常見的環(huán)境,現(xiàn)在一般的 Linux 虛擬主機(jī)都采用這種環(huán)境。新建一個(gè) htaccess.txt 文件,添加下面的代碼:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
然后上傳到 WordPress 站點(diǎn)的根目錄,重命名為 .htaccess 即可
Nginx偽靜態(tài)規(guī)則
Nginx環(huán)境一般是Linux 主機(jī) VPS或服務(wù)器用戶用的比較多,這些用戶一般都會(huì)自己配置Nginx,或者有專門的人幫你配置,打開 nginx.conf 或者某個(gè)站點(diǎn)的配置環(huán)境,比如 wpdaxue.com.conf(不同人配置的不一樣),在 server { } 大括號(hào)里面添加下面的代碼:
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
保存,重啟 Nginx 即可。
注:如果以上規(guī)則不適用,請(qǐng)自己查看WP官方文檔:https://codex.wordpress.org/Nginx
題外話:倡萌一直不推薦在 windows 的IIS服務(wù)器下安裝 WordPress,因?yàn)?IIS 環(huán)境運(yùn)行php程序的效率,相對(duì)同等配置下 Linux 的 Apache 和 Nginx 環(huán)境,要低的多,具體可以看看http://www.ydqwiac.cn/wordpress-hosting.html





不會(huì)配置08影院的偽靜態(tài) 有人會(huì)的么?
我的也是用的便宜的WIN主機(jī),靜態(tài)化,后面必須有index.php,看著百度的教程弄什么404也沒弄好,最后自己沒轍了,挨個(gè)的找的源文件修改的:
步驟:1.固定鏈接選擇第一個(gè),默認(rèn)的 ?p=123這樣的
2.更改.htaccess RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteRule ^cat-([^/]+).html ?cat=$1
RewriteRule ^page_id-([^/]+).html ?page_id=$1
RewriteRule ^p-([^/]+).html ?p=$1
樓主的上面的那個(gè)第7行,一定不要有,那個(gè)會(huì)把自己寫的規(guī)則導(dǎo)入到首頁··就是RewriteRule . /index.php [L] 這個(gè)不要;
3.在wp-includes 》link-template.php 這個(gè)文件下搜索”?p=”,”?page_id=”就是改成默認(rèn)固定鏈接之后,前臺(tái)文章基本頁面,以及page頁面的鏈接;找到之后改成“p-””page_id-” ,在加個(gè)“.html”;如:$permalink = home_url(‘p-‘ . $post->ID.’.html’);本來里面的”.html”是沒有的,自己加上$link = home_url( ‘page_id-‘ . $post->ID.’.html’ );這是另外的一個(gè)。
4.?cat= 分類的鏈接是這個(gè)的,但是在上面的3中找不到,后來想的辦法是把導(dǎo)航的鏈接進(jìn)行更改,文件是:wp-includes>nav-menu-template.php 這個(gè)文件里面,需要自己寫一段PHP代碼。首先找到:
$value = ( ‘href’ === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ‘ ‘ . $attr . ‘=”‘ . $value . ‘”‘;
這兩行代碼;然后在他們中見插入一個(gè)if判斷來處理鏈接字符
if($value!=’/’){
$subV=str_replace(“?”,””,$value);
$reV=str_replace(“=”,”-“,$subV);
$value=$reV.”.html”;
}
更改后是:$value = ( ‘href’ === $attr ) ? esc_url( $value ) : esc_attr( $value );
if($value!=’/’){
$subV=str_replace(“?”,””,$value);
$reV=str_replace(“=”,”-“,$subV);
$value=$reV.”.html”;
}
$attributes .= ‘ ‘ . $attr . ‘=”‘ . $value . ‘”‘;
4.通過上述方法頁面上的鏈接都會(huì)以html展現(xiàn)
5.但是在文章列表頁面會(huì)出現(xiàn)翻頁無法翻頁的情況,原因是翻頁只能用動(dòng)態(tài)的進(jìn)行搜索?cat=2 ,這樣的···wordpress的翻頁是直接獲取當(dāng)前的列表的url然后在那個(gè)基礎(chǔ)上進(jìn)行查詢。建議如果有時(shí)間可以自己寫個(gè)翻頁的代碼,或者按照如下方法·····(我弄了一天的時(shí)間···這幾個(gè)問題···醉了)
首先,我用的是wp-pagenavi 這個(gè)插件弄的翻頁··原始的太丑···;通過FTP找到插件的目錄:wp-content/plugins/wp-pagenavi/core.php 這個(gè)里面的269行:$attr[‘href’]= $this->get_url( $page );這行,改成
$attr[‘href’] = “javascript:void(0);”;
$attr[‘data-href’] = $this->get_url( $page );
$attr[‘onclick’] = “navPage(this)”;
第一行是把原來的href弄成空鏈接,這樣前臺(tái)的翻頁按鈕就不會(huì)跳轉(zhuǎn)頁面;
第二行是把url寫入data-href 自定義屬性··用來存儲(chǔ)url
第三行,添加··onclick方法里面的函數(shù)是我自己寫的,下面會(huì)提到;
5.然后找到footer.php,寫入js . 這個(gè)文件在主題文件夾里面,當(dāng)然你可以隨意找個(gè)公共頁面,或者寫在歸檔archive.php這個(gè)里面;JS如下:
function navPage(e){
var httpV=e.getAttributeNode(“data-href”).value;
var patt=/paged/;
var result=patt.test(httpV);
if(result==true){
console.log(httpV);
var n=httpV.replace(“cat-“,”?cat=”);
n=n.replace(“.html?”,”&”);
window.location.replace(n);
}else{
e.getAttributeNode(“href”).value=httpV;
}
}
這個(gè)我就不解釋了,就是把翻頁的a里面的date-href數(shù)據(jù)拿出來然后改成動(dòng)態(tài)的,然后加載頁面,完事判斷在動(dòng)態(tài)鏈接的情況下直接讀取data-href插入到href進(jìn)行跳轉(zhuǎn)··以上做個(gè)總結(jié)
菜鳥學(xué)習(xí)一下
求大學(xué)解答,我的主機(jī)是win,用httpd.ini和htaccess設(shè)置了偽靜態(tài),去掉了固定連接中的index.php,但是點(diǎn)擊回復(fù)某個(gè)評(píng)論時(shí),鏈接里又出現(xiàn)了index.php,導(dǎo)致指向404.
LZ,問下兩個(gè)問題:
1、我的nginx.conf文件沒有server {},自己手動(dòng)添加后用 nginx -t提示語法錯(cuò)誤。
2、service httpd start命令不能重啟nginx服務(wù),提示如下問題。
Starting httpd: httpd: Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
nginx 的版本有問題,