.htaccess 可以改變 Apache 對目錄的設定, 其中一個十分常用的地方是轉址, 以下是 .htaccess 常用轉址例子:
301 永久轉址:
1 |
Redirect 301 / http://www.newdomain.com/ |
302 臨時轉址
1 |
Redirect 302 / http://www.newdomain.com/ |
將檔案轉到指定檔案位置
1 |
Redirect /olddirectory/oldfile.html http://www.domain.com/newdirectory/newfile.html |
將沒有 www 的網址轉到 www 開頭網址:
1 2 3 |
RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] |
將 www 開頭的網址轉到沒有 www 網址:
1 2 3 |
RewriteEngine On RewriteCond %{HTTP_HOST} !^my-domain\.com$ [NC] RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L] |
將所有不是 new-domain.com 轉到 new-domain.com
1 2 3 |
RewriteEngine On RewriteCond %{HTTP_HOST} !^www\.new-domain\.com$ [NC] RewriteRule ^(.*)$ http://www.new-domain.com/$1 [L,R=301] |