打造高品質環境,創造潔淨生活
專業承包裝潢清潔、石材保養、拋光磚
防水抓漏外包網專業維修各大品牌冷氣,多家大廠指定合作,各式問題排除
完修有保固,證照技師團隊,高效率服務

首頁  •  j2h 論壇 • 程式設計討論     • 

php?id=xxxx 都變成了 .html

房東:小小
發表時間:2006-12-25


apache的rewite engine這個模組
把 php?id=xxxx 都變成了 .html
mod_rewrite被用作seo的利器,
在joomla!當中有一項功能叫sef,就是用mod_rewrite做的
http://202.99.120.116:82/gate/big5/publish.it168.com/2005/0919/20050919003301.shtml
http://72.14.235.104/search?q=cache:dawuJNET7lUJ:phorum.study-area.org/viewtopic.php%3Fp%3D141724%26sid%3D76fe576f4c0c63033461c80dd0b5fb56+Apache2+mod_rewrite%E8%A8%AD%E5%AE%9A&hl=zh-TW&gl=tw&ct=clnk&cd=8
mod_rewrite 是一個強大的 URL 重導向模組。因為他使用正規表示式作為比對規則的方法,因此幾乎提供無限的可能性,就看你怎麼活用正規表示式。

設定內容

PLAIN TEXTCODE:

RewriteEngine On RewriteRule ^/zope(.*) http://localhost:8080/VirtualHostBase/http/www.pwchi.info:80/zope/VirtualHostRoot/_vh_zope$1 [L,P]

RewriteEngine 是用來啟動或關閉 Rewrite 功能,預設為 Off 。
因此必須先用 RewriteEngine On 來開啟 Rewrite 功能。

RewriteRule 是實際進行 URL 重導向的語法。語法如下:
RewriteRule <比對規則> <要導向的網址> [旗標]

比對規則中的 ^/zope(.*) 是一個正規表示式,代表只要請求的 URL 中有 /zope 就算符合。

當符合規則時,就會被重新導向到新的網址。

$1 表示將 /zope 以後的字串,接在替換後的URL後面。
[L,P] 則是控制旗標
L 表示立即停止規則的運算,如果後面有其他的 RewriteRule 規則設定,就不會被干擾。

P 表示將替換後的 URL 提出代理請求。為了不讓使用者察覺不同,如果不加 P ,瀏覽器網址列會直接被指向新網址。





DocumentRoot 絕對路徑
DirectoryIndex index.php index.htm
ServerName www.7taste.com
ErrorLog logs/www_7_com_error.log
CustomLog logs/www_7_com_access common
ServerAdmin [email protected]
RewriteEngine On
RewriteRule ^/index.html /index.php
RewriteRule ^/index([0-9]+).html$ /index.php?pl=$1
RewriteRule ^/page([0-9]+).html$ /index.php?page=$1
RewriteRule ^/ct([0-9]+).html$ /index.php?ct1=$1
RewriteRule ^/page_([0-9]+)_([0-9]+).html$ /index.php?pl=$1&ct1=$2
RewriteRule ^/index_c_([0-9]+)_([0-9]+).html$ /index.php?ct1=$1&ct2=$2
#RewriteRule ^/([0-9]+).html$ /index.php?pl=$1
#RewriteRule /name([0-9]+) /index\.php\?pl=$1













# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride All



  • 贊助網站       

    廣利不動產-新板特區指名度最高、值得您信賴的好房仲
    您的托付,廣利用心為您服務
    廣利不動產-板橋在地生根最實在--新板特區指名度最高、值得您信賴的好房仲
    完整房訊,房屋、店面熱門精選物件,廣利不動產 優質仲介,房屋租賃、買賣資訊透明,交易真安心!

  • 1 樓住戶:怪老伯
    發表時間:2007-01-03

    今天把討論區頁面改靜態
    用Discuz!官方的方式來設定會出現404
    沒辦法用?這搜尋引擎比較好抓說∼
    我是使虛擬主機,可能沒有開功能
    但是普通人的小地方就可以使用
    那就如法泡製,成功了,可以用了,對網站優化又進了一步
    寫一下改法
    官方版本是


    # 將 RewriteEngine 模式打開
    RewriteEngine On

    # 修改以下語句中的 /discuz 為你的論壇目錄地址,如果程序放在根目錄中,請將 /discuz 修改為 /
    RewriteBase /discuz

    # Rewrite 系統規則請勿修改
    RewriteRule ^archiver/([a-z0-9\-]+\.html)$ archiver/index.php?$1
    RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2
    RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ viewthread.php?tid=$1&extra=page\%3D$3&page=$2
    RewriteRule ^profile-(username|uid)-(.+)\.html$ viewpro.php?$1=$2



    RewriteEngine on
    RewriteRule ^archiver/([a-z0-9\-]+\.html)$ archiver/index.php?$1
    RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2
    RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ viewthread.php?tid=$1&extra=page\%3D$3&page=$2
    RewriteRule ^profile-(username|uid)-(.+)\.html$ viewpro.php?$1=$2


    2 樓住戶:小優
    發表時間:2007-01-21

    真對phpBB的一個Apache mod_rewrite配置樣例︰
    mod_rewrite和isapirewrite基本兼容,但是還是有些不同,比如︰isapirewrite中"?"需要轉義成"\?",mod_rewrite不用,isapirewrite支持 "\d+" (全部數字),mod_rewrite不支持
    RewriteEngine On
    RewriteRule /forum/topic_(.+)\.html$ /forum/viewtopic.php?t=$1 [L]
    RewriteRule /forum/forum_(.+)\.html$ /forum/viewforum.php?f=$1 [L]
    RewriteRule /forum/user_(.+)\.html$ /forum/profile.php?mode=viewprofile&u=$1 [L]


    3 樓住戶:高手
    發表時間:2007-01-26

    一下是真對phpBB的一個Apache mod_rewrite配置樣例︰
    mod_rewrite和isapirewrite基本兼容,但是還是有些不同,比如︰isapirewrite中"?"需要轉義成"\?",mod_rewrite不用,isapirewrite支持 "\d+" (全部數字),mod_rewrite不支持
    RewriteEngine On
    RewriteRule /forum/topic_(.+)\.html$ /forum/viewtopic.php?t=$1 [L]
    RewriteRule /forum/forum_(.+)\.html$ /forum/viewforum.php?f=$1 [L]
    RewriteRule /forum/user_(.+)\.html$ /forum/profile.php?mode=viewprofile&u=$1 [L]

    4 樓住戶:高手
    發表時間:2007-01-27

    Apache 特有的 rewrite 技術很好用,Vovo2000.Com 的
    /gallery/
    /vovo-gallery/
    /phpbb2/
    /linka/
    /paintbbs/ paintbbs-archive
    ...
    都已經徹底嵌入 rewrite。

    Rewrite 很簡單的說,就是把類似
    ==> /phpbb2/viewtopic.php?t=1388&start=0&sort=0
    動態網頁,讓他給 user 的感覺是
    ==> /phpbb2/viewtopic-1388.html
    而,實際上還是執行 viewtopic-1388.html&start=0&sort=0

    再舉一個例子:
    使用者看到 => /paintbbs/pch-viewer-1234.html
    實際上執行 => /paintbbs/pch-viewer.cgi?pic=1234.png&pch=1234.pch

    對 User 來說他只需要記得
    Viewtopic-1388.html 或 pch-viewer-1234.html 即可


    好處有:
    1. URL 短一點,對使用者而言,靜態網址的接受度較高。
    2. Search Engine Robots/Spiders/Crawlers 比較吃的下去
    3. (未經證實)靜態網址的 SEO/SERP 效果好
    4. 減少您 Log File / header流量的負擔,積少成多

    要注意:
    1. 要注意 Session ID 的控制或 Cache-Control 等議題
    2. 一些使用者專屬個人資料的頁面,rewrite 時要注意。
    3. .htaccess 的設定

    ===========================================
    ===========================================
    在這裡舉 PHPBB2 2.0.3 的例子來說,一步一步來:

    1. 把 Apache 的 Rewrite 打開(mod_rewrite)

    2. 在 phpbb/include/page_header.php
    的 // Generate logged in/logged out status 之前加入
    代碼:

    ob_start();
    function replace_for_mod_rewrite(&$s)
    {
    $urlin = array(
    "'(? "'(? "'(? "'(? "'(? "'(? "'(? "'(? "'(? "'(? );
    $urlout = array(
    "viewforum-$1-$2-$3.html",
    "forum-$1.html",
    "forum-$1.html",
    "prev-topic-$1.html",
    "next-topic-$1.html",
    "viewtopic-$1-$2-$3-$4.html",
    "viewtopic-$1-$2.html",
    "viewtopic-$1.html",
    "viewpost-$1.html",
    "profile-$1.html"
    );
    $s = preg_replace($urlin, $urlout, $s);
    $s = mb_ereg_replace( "viewtopic-([0-9]+).html&(.*)$", "viewtopic.php?t=\\1&\\2", $s);
    return $s;
    }


    3. 在 phpbb/include/page_tail.php

    代碼:
    在下列?#123;式之後:
    $db->sql_close();

    ?#91;入:
    $contents = ob_get_contents();
    ob_end_clean();
    echo replace_for_mod_rewrite($contents);
    global $dbg_starttime;


    在下列?#123;式之後:
    ob_end_clean();

    ?#91;入:
    echo replace_for_mod_rewrite($contents);
    global $dbg_starttime;



    4. 接下來當然要建立 .htaccess 的 rewrite rule

    代碼:
    RewriteEngine On
    RewriteRule ^viewforum-([0-9a-zA-Z]+)-([0-9]+)-([0-9]+)\.html$ viewforum.php?f=$1&topicdays=$2&start=$3
    RewriteRule ^forum-([0-9a-zA-Z]+)\.html$ viewforum.php?f=$1&mark=topic
    RewriteRule ^forum-([0-9a-zA-Z]+)\.html$ viewforum.php?f=$1
    RewriteRule ^prev-topic-([0-9]+)\.html$ viewtopic.php?t=$1&view=previous
    RewriteRule ^next-topic-([0-9]+)\.html$ viewtopic.php?t=$1&view=next
    RewriteRule ^viewtopic-([0-9]+)-([0-9]+)\.html$ viewtopic.php?t=$1&start=$2
    RewriteRule ^viewtopic-([0-9]+)-([0-9]+)-([a-zA-Z]*)-([0-9]+)\.html$ viewtopic.php?t=$1&postdays=$2&postorder=$3&start=$4
    RewriteRule ^viewtopic-([0-9]+)\.html$ viewtopic.php?t=$1
    RewriteRule ^viewpost-([0-9]+)\.html$ viewtopic.php?p=$1
    RewriteRule ^viewtopic-([0-9]+)\.html&highlight=(.*)$ viewtopic.php?t=$1&highlight=$2
    RewriteRule ^profile-([0-9]+)\.html$ profile.php?mode=viewprofile&u=$1


    5. 測試∼ 當然 OK;

    再分享一下 Linka 的 rewrite .htaccess

    代碼:

    RewriteEngine On
    RewriteBase /linka/
    RewriteRule ^category/([0-9]+)\.html$ http://vovo2000.com/linka/links.php?cat=$1 [QSA]
    RewriteRule ^action/([a-z0-9]+)\.html$ http://vovo2000.com/linka/links.php?action=$1 [QSA]
    RewriteRule ^view/([a-z0-9]+)/([0-9]+)\.html$ http://vovo2000.com/linka/links.php?action=$1&link_id=$2 [QSA]
    RewriteRule ^adminvlink/$ http://vovo2000.com/linka/ [R]




    供各位參考,善用 Rewrite 讓您的動態網站更美觀、更一致、更方便被「吃」

    5 樓住戶:高手
    發表時間:2007-01-27

    使用上很簡單,看mod_rewrite官方說明,應該就可以明白,要不也可以利用Google找到很多教學(關鍵字:mod_rewrite tutorial)。

    要特別注意的一點,得記得把該目錄的 AllowOverride 設置為 All (Apache設定檔裡面)。
    如果是 None 的話,mod_rewrite 不會生效。

    像我的CentOS預設就是把 /var/www/html 的 AllowOverride 設置為 None,所以得去編輯 /etc/httpd/conf/httpd.conf ,找到 ... 裡面的 AllowOverride,改為 All,再重新啟動 httpd 即可。


    6 樓住戶:高手
    發表時間:2007-01-27

    修改 apache 下的 http.conf
    搜尋並將以下兩行前的注解「#」拿掉

    #LoadModule rewrite_module modules/mod_rewrite.so
    #AddModule mod_rewrite.c

    再搜尋 http.conf 中有

    AllowOverride None
    的語句,將其改為

    AllowOverride All


    然後重啟 Apache Server, 大功告成!

    7 樓住戶:妹子
    發表時間:2007-01-29

    http://203.68.102.46/online_book/content.php?chapter_sn=200

    Apache/2.0.55 (Win32) PHP/5.1.1

    操作方法如下:

    用文字編輯器(我推薦notepad++)開啟httpd.conf

    重要:變更前請先備份原httpd.conf檔,以免不小心掛了你的Server

    搜尋以下文字,

    LoadModule rewrite_module modules/mod_rewrite.so

    我的版本預設是開啟的,所以只要確認前面沒有註解的#字號即可。

    再搜尋以下的文字

    AllowOverride None

    把它變更為

    AllowOverride All 即可

    總共是四個Part

    變更部分如下

    ————————————————————————-

    Options FollowSymLinks ExecCGI Indexes
    AllowOverride All

    ————————————————————————- #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be “All”, “None”, or any combination of the keywords:
    # Options FileInfo AuthConfig Limit
    #
    AllowOverride All

    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be “All”, “None”, or any combination of the keywords:
    # Options FileInfo AuthConfig Limit#

    ————————————————————————-

    #Alias /icons/ “d:/server//Apache/icons/”


    Options Indexes MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all

    ————————————————————————-

    #
    # “C:/Program Files/Apache Group/Apache2/cgi-bin” should be changed to whatever your ScriptAliased
    # CGI directory exists, if you have that configured.
    #

    AllowOverride All
    Options None
    Order allow,deny
    Allow from all
    ————————————————————————-

    重新啟動Apache,再設定Use Friendly URLS:、Navigation Root:
    按下update options即可


    8 樓住戶:達人
    發表時間:2007-01-29

    2.X
    http://httpd.apache.org/docs-2.0/mod/mod_rewrite.html
    1.3.X
    http://httpd.apache.org/docs/mod/mod_rewrite.html

    把這一頁用力看完,您就會懂了,這頁有很多東西很有用

    9 樓住戶:夜店小弟
    發表時間:2007-01-30

    apache1.3.3 3:
    httpd.conf 文件
    将其中的
    LoadModule rewrite_module modules/mod_rewrite.so
    AddModule mod_rewrite.c
    前面的注释#去掉
    将下面的语句加到最后

    CODE:


    DocumentRoot 此处修改为TT的绝对路径
    DirectoryIndex index.php index.htm
    ServerName www.7taste.com
    ErrorLog logs/www_7_com_error.log
    CustomLog logs/www_7_com_access common
    ServerAdmin cailinzhengo163.com
    RewriteEngine On
    RewriteRule ^/index.html /index.php
    RewriteRule ^/index([0-9]+).html$ /index.php?pl=$1
    RewriteRule ^/page([0-9]+).html$ /index.php?page=$1
    RewriteRule ^/ct([0-9]+).html$ /index.php?ct1=$1
    RewriteRule ^/page_([0-9]+)_([0-9]+).html$ /index.php?pl=$1&ct1=$2
    RewriteRule ^/index_c_([0-9]+)_([0-9]+).html$ /index.php?ct1=$1&ct2=$2
    #RewriteRule ^/([0-9]+).html$ /index.php?pl=$1
    #RewriteRule /name([0-9]+) /index\.php\?pl=$1





     共 9 人回應  選擇頁數 【第1 頁】 

    姓名:
    佈告內容:
    其他選項: