面向搜索引擎的URL優(yōu)化_PHP教程
推薦:PHP空白頁(yè)面常見(jiàn)原因及解決方法編寫PHP,難免會(huì)出現(xiàn)錯(cuò)誤。其實(shí)出現(xiàn)錯(cuò)誤也不難解決,最難解決的是出現(xiàn)空白頁(yè)面。大家想想看,若編寫PHP出現(xiàn)錯(cuò)誤,可以根據(jù)錯(cuò)誤的提示來(lái)改正,倘若PHP什么也不給你顯示,那豈不是讓編寫者困撓不
我在寫晉遠(yuǎn)信息網(wǎng)(www.geofuture.net)時(shí)沒(méi)有考慮到URL如何面向搜索引擎優(yōu)化,完成了以后才開始優(yōu)化的。這時(shí)要考慮一方面要靜態(tài)的URL,一方面要盡量保持原有的程序不變以便于以后維護(hù),所以就采用url_rewrite的方法。下面是httpd.conf中相應(yīng)的部分
| 以下為引用的內(nèi)容: <VirtualHost *:80> DocumentRoot /home/geofuture ServerName www.geofuture.net ServerAlias *.geofuture.net geofuture.net *.cic123.com DirectoryIndex index.files index.html index.php RewriteEngine On #RewriteLog logs/rewrite.log #RewriteLogLevel 9 #RewriteRule / http://geofuture.vicp.net/ [L] RewriteCond %{REQUEST_FILENAME} index.files [OR] RewriteCond %{REQUEST_FILENAME} error.files [OR] RewriteCond %{REQUEST_FILENAME} cat.files [OR] RewriteCond %{REQUEST_FILENAME} area.files [OR] RewriteCond %{REQUEST_FILENAME} info.files [OR] RewriteCond %{REQUEST_FILENAME} topic.files [OR] RewriteCond %{REQUEST_FILENAME} post.files [OR] RewriteCond %{REQUEST_FILENAME} profile.files [OR] RewriteCond %{REQUEST_FILENAME} userinfo.files [OR] RewriteCond %{REQUEST_FILENAME} cert.files [OR] RewriteCond %{REQUEST_FILENAME} revise.files [OR] RewriteCond %{REQUEST_FILENAME} review.files [OR] RewriteCond %{REQUEST_FILENAME} feedback.files [OR] RewriteCond %{REQUEST_FILENAME} keyrank.files [OR] RewriteCond %{REQUEST_FILENAME} rss.files RewriteRule ^(. ?)\Q(.*)$ $1=$2 [N] RewriteRule ^(. ?)ZZ(. )$ $1&$2 [N] RewriteRule ^(. ?)\.files(.*)$ $1.php$2 [N] RewriteRule ^(. ?)\.php(/|&)(. ).html$ $1.php?$3 [L] ErrorDocument 400 /error.php?status=400 ErrorDocument 401 /error.php?status=401 ErrorDocument 403 /error.php?status=403 ErrorDocument 404 /error.php?status=404 ErrorDocument 405 /error.php?status=405 ErrorDocument 408 /error.php?status=408 ErrorDocument 410 /error.php?status=410 ErrorDocument 411 /error.php?status=411 ErrorDocument 412 /error.php?status=412 ErrorDocument 413 /error.php?status=413 ErrorDocument 414 /error.php?status=414 ErrorDocument 415 /error.php?status=415 ErrorDocument 500 /error.php?status=500 ErrorDocument 501 /error.php?status=501 ErrorDocument 502 /error.php?status=502 ErrorDocument 503 /error.php?status=503 ErrorDocument 506 /error.php?status=506 </VirtualHost> |
我只把以內(nèi)容為主的欄目?jī)?yōu)化了,至于登錄和搜索等部分保持不變,因?yàn)檫@些部分也是搜索引擎不關(guān)心的。同時(shí),一些常見(jiàn)的狀態(tài)碼(HTTP Status)也有相應(yīng)的靜態(tài)頁(yè)面。上面只修改了外來(lái)請(qǐng)求的URL,而程序生成的URL通過(guò)下面的代碼實(shí)現(xiàn):
| 以下為引用的內(nèi)容: <?php //-------------- SECTION NAME ----------------------------------- // 動(dòng)態(tài)URL改寫成靜態(tài) function url_rewrite($buffer) { //return $buffer; $search = array( '.php', '?', '&', 'filesZZ', '=', 'hrefQ', ); $replace = array( '.files', '/', 'ZZ', 'files/ZZ', 'Q', 'href=', ); preg_match_all('/href="\/(index|error|cat|area|info|topic| post|profile|userinfo|cert| revise|review|feeback|keyrank|rss) \.php(.*?)"/', $buffer, $match); $url = str_replace($search, $replace, $match[0]); $url = preg_replace('/\.files(. ?)(#. ?)?"$/', '.files\\1.html\\2"', $url); return str_replace($match[0], $url, $buffer); } ?> |
上面的函數(shù)定義放在公共頭文件里面。然后,在需要優(yōu)化的頁(yè)面開始處加入以下代碼:
ob_start("url_rewrite");
這也是原來(lái)的程序唯一需要修改的地方。這樣做還有一個(gè)問(wèn)題。 例如我寫的分頁(yè)函數(shù)等代碼, 都要求原來(lái)動(dòng)態(tài)的URL, 而優(yōu)化了以后通過(guò) $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'] 就只能得到優(yōu)化以后的靜態(tài)URL, 所有依賴于動(dòng)態(tài)URL的代碼都得修改一下, 以能夠處理原來(lái)的URL和優(yōu)化的。這樣得改寫公共頭文件的許多代碼。所以我就想了一個(gè)迂回的辦法:
| 以下為引用的內(nèi)容: <?php //-------------- SECTION NAME ----------------------------------- // 恢復(fù)成動(dòng)態(tài)URL function url_resume($url) { $search = array( '.files', '.php/', 'ZZ', '/ZZ', 'Q', '.html', ); $replace = array( '.php', '.php?', '&', '?&', '=', '', ); $url = str_replace($search, $replace, $url); return $url; } ?> |
有了上面的函數(shù),在需要?jiǎng)討B(tài)URL的地方只要調(diào)用一下就可以了,而不需重寫各個(gè)核心函數(shù)。需要注意的是,url_rewrite和url_resume雖然是相反的過(guò)程,但是它們的參數(shù)是不同的。前者的參數(shù)是整個(gè)緩沖區(qū),后者只是一個(gè)單個(gè)的URL,因?yàn)榛謴?fù)的情況畢竟很少,而全局處理重寫可以提高速度。
分享:PHP編程中break及continue兩個(gè)流程控制指令講解一下PHP腳本編程中 break 及 continue 兩個(gè)流程控制指令。 break 用來(lái)跳出目前執(zhí)行的循環(huán),如下例 以下為引用的內(nèi)容: <?php $i = 0; while ($
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁(yè)面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對(duì)圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問(wèn)控制的和運(yùn)算符優(yōu)先級(jí)介紹
- 關(guān)于PHP語(yǔ)言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國(guó)語(yǔ)言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
PHP教程Rss訂閱編程教程搜索
PHP教程推薦
- WordPress禁止輸出錯(cuò)誤信息設(shè)置方法
- php獲取$_POST同名參數(shù)數(shù)組的實(shí)現(xiàn)介紹
- PHP打印一個(gè)三角形
- PHP MYSQL實(shí)例:網(wǎng)站在線人數(shù)的程序代碼
- php配置文件php.ini的中文注釋版
- 關(guān)于php 接口問(wèn)題(php接口主要也就是運(yùn)用curl,curl函數(shù))
- PHP導(dǎo)出Excel 之 Spreadsheet_Excel_Writer
- 解析基于MVC的輕量級(jí)PHP框架
- PHP編程中分頁(yè)顯示實(shí)例代碼
- Zend Framework 入門——錯(cuò)誤處理
- 相關(guān)鏈接:
- 教程說(shuō)明:
PHP教程-面向搜索引擎的URL優(yōu)化
。