bency@jccf:www 02:07:51 $ git clone https://github.com/moztw/central.git Cloning into 'central'... remote: Counting objects: 28252, done. remote: Compressing objects: 100% (11586/11586), done. remote: Total 28252 (delta 15531), reused 28252 (delta 15531) Receiving objects: 100% (28252/28252), 248.23 MiB | 760 KiB/s, done. Resolving deltas: 100% (15531/15531), done. bency@jccf:www 02:13:57 $因為是virtual host的關係,所以也要建立相關的設定檔,就叫做firefox吧(/etc/apache22/site-available/firefox)
<VirtualHost *:80> ServerAdmin bency80097@gmail.com DocumentRoot "/var/www/central/htdocs" <Directory "/var/www/central/htdocs"> Options FollowSymLinks AllowOverride ALL Order allow,deny Allow from All </Directory> ServerName firefox.bency.org Errorlog "/var/log/apache2/firefox-error.log" CustomLog "/var/log/apache2/firefox-access.log" common </VirtualHost>這是我一開始的設定檔,如果有先看過.htaccess,還是看不出來這設定檔哪裡有問題的話,那我們等級應該是差不多的,一重開apache馬上就碰到500老師叫你聯絡網站管理員,但你就是那位管理員阿!這時看error log會告訴你
/var/www/central/htdocs/.htaccess: Option MultiViews not allowed here明明就AllowOverride已經設定是ALL啦,應該每個設定都可以在.htaccess裡面設定吧?但並不包含Option裡面的 MultiViews,所以需要另外指定,也只有這個沒包含在ALL裡面,這也是一件很奇怪的事,加上之後再重啟apache,你會發現伍佰老師還是沒走,這時error log告訴我們另一件事
/var/www/central/htdocs/.htaccess: AddType not allowed here原來AddType是FileInfo在處理,所以在AllowOverride後面加上FileInfo,然後再重啟apache,然後再碰到伍佰,這次是另一個錯誤
/var/www/central/htdocs/.htaccess: DirectoryIndex not allowed here太棒了,再加上Indexes吧,既然是專案要的,那就給他吧,其實從解決MultiViews之後,只要加上ALL就可以避免掉後面AddType跟DirectoryIndex這兩個錯誤,不過為了安全,還是不要開啟網站不需要的功能比較好。
不過阿,雖然沒錯誤了,但網頁版面看起來怎麼怪怪的,好像中風一樣,看起來歪歪怪怪的,跟原版不一樣,原來是因為有使用了SSI的關係,SSI拆括號之後會變成Server-Side Inclues,是一個可以讓html可以讀外部html檔的功能,換句話說就像模版一樣,可以把網頁分成不同檔案,再由某個檔案include進來,因為apache預設沒開啟的關係,所以碰到以這方式架設的網站會出包子,如果連首頁都無法顯示,反而是直接顯示檔案列表的話,那在.httaccess或是你知道可以讓apache讀到的設定檔裡面,加上這個mime type:
AddType text/html .shtml這樣就會把首頁index.shtml當做html來解讀,當然也可以在DirectoryIndex 後面加上 .shtml
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm這也是另一個解法,只是這樣就變成全站通用的設定,看自己定奪囉。 那現在來解決排版不對的問題,其實可以比較一下瀏覽器上看到的原始碼,可以發現結構差很多,有很多結構都不見了,應該說,是沒有被讀進來,很明顯就是SSI出了包子,要開啟SSI需要mod_include.so這個模組,請依照自己用的distro讓apache能啟用這個模組(不知道有沒有開啟的話,可以用phpinfo()來看,用appserv的話,預設是有phpinfo.php可以看,用瀏覽器打開就會列出了),然而啟用這模組之後還是沒解決問題,這時只要在.htaccess的Option加上+Includes就可以了
Options +MultiViews +Includes這樣就可以搞定了,這次是真的完成了,版面完整,且功能也都正常,以下是我改完可以用的兩個設定檔,供大家參考
virtual host:
<VirtualHost *:80> ServerAdmin bency80097@gmail.com DocumentRoot "/var/www/central/htdocs" <Directory "/var/www/central/htdocs"> Options FollowSymLinks AllowOverride FileInfo Indexes Options=All,MultiViews Order allow,deny Allow from All </Directory> ServerName firefox.bency.org Errorlog "/var/log/apache2/firefox-error.log" CustomLog "/var/log/apache2/firefox-access.log" common </VirtualHost>.htaccess:
Options +MultiViews +Includes AddType video/ogg .ogv AddType video/mp4 .mp4 AddType video/webm .webm AddType text/rdf+n3 .n3 #It's timbl's fault that this is not yet standardized AddType text/html .shtml DirectoryIndex index Overview參考來源:
http://mathiasbynens.be/notes/apache-allowoverride-all
http://ircarchive.info/apache/2007/3/9/34.html
http://mail-archives.apache.org/mod_mbox/httpd-users/200204.mbox/%3C3CB6A294.974F601@bourse.ch%3E