Welcome! 登入 註冊
美寶首頁 美寶百科 美寶論壇 美寶落格 美寶地圖

Advanced

Apache URL Rewriting: 讓網址簡化改為靜態(HTML)的設定(httpd.conf)

Posted by Hsinping 
功能:簡化或縮短網址。

例子:

原網址 http://mepopedia.com/forum/list.php?146
簡化後 http://mepopedia.com/forum/146

原網址 http://mepopedia.com/forum/read.php?146,419
簡化後 http://mepopedia.com/read_146_419.html

方法:

1. 確定 Apache web server 有設定為支援 ReWrite(預設是有):

 確認在設定檔中有「LoadModule rewrite_module modules/mod_rewrite.so」
 一般而言,設定檔是在 httpd.conf 中。
 路徑可能是 /usr/local/apache2/conf/httpd.conf 或 /etc/apache2/vhosts.d/vhost.conf 等。

2. 在設定 DocumentRoot 的 中加入以下設定:

 <Directory "/usr/local/apache2/...">
  ......
  RewriteEngine On
  RewriteRule ^forum/([0-9]+)$ /forum/list.php?$1 [L]
  RewriteRule ^read_([0-9]+)_([0-9]+)\.html$ /forum/read.php?$1,$2 [L]
  ......
 </Directory>

 其中更改網址的指令是以 regular expression 為主。

3. rcapache2 restart (重啟Apache server)


有關詳細的指令說明,請見以下參考資料。


1. URL Rewriting Guide - Apache HTTP Server Version 2.0
2. 使用 Apache 的 ReWrite 設定讓 URL 變短(short URL)、變成靜態樣式
3. apache mod_rewrite - URL Rewriting OHaHa's學習心得 (2008)