每次設定轉址規則都會忘記怎麼做,在這邊簡單筆記一下。

 

首先要先在 Apache 設定 httpd.conf,重點是要設定 "AllowOverride All"。

<Directory "D:/test/">
        Options Indexes FollowSymLinks MultiViews ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
</Directory>

 

 

然後在資料夾裡新增一個文字檔,檔名是 ".htaccess",內容我這麼寫:

RewriteEngine on
# 不管使用者輸入 http://mydomain.com.tw 或 http://www.mydomain.com.tw,一律導向 http://www.mydomain.com.tw,以利日後程式處理 & SEO 集中
RewriteCond %{HTTP_HOST} ^mydomain.com.tw$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com.tw/$1 [R=301,L]

# 原本使用者進入首頁會連入 index.html/index.htm,改為強制導向 index.php
RewriteCond %{REQUEST_URI} ^.*/index.html?$ [NC]
RewriteRule ^(.*)$ /index.php [R=301,L]

# 自訂 HTTP 404 頁面,當使用者發生 404 問題時移向專用搜尋頁,提供其他建議結果
ErrorDocument 404 /search.php

<Files .htaccess>
    order allow,deny
    deny from all
</Files>

設定完畢後要記得重啟 Apache,才會生效。

 

過程中在嘗試設定目錄權限時,噴出錯誤訊息 "couldn't perform authentication. AuthType not set!"。網頁會顯示為 HTTP 500,在 php 的 error.log 沒有訊息,但在 Apache 的 error.log 會看到這行:

[Mon Jun 23 11:28:19 2014] [crit] [client 127.0.0.1] configuration error:  couldn't perform authentication. AuthType not set!: index.php

上網查了一下解決方法,說是除了 Apache 2.4 版以外,都不要在 <Directory> 區段加 "Require all granted",應該改用 "Allow from all"。

 

詳細的 Rewrite 相關語法,可以參考 KaiYai & David Kuo 的《URL Rewrite(網址重寫)語法教學》

arrow
arrow
    文章標籤
    apache error
    全站熱搜

    小攻城師 發表在 痞客邦 留言(1) 人氣()