如何在 apache2 建立轉址機制

How To Create Temporary and Permanent Redirects with Apache

什麼是重定向 (redirect)

通常,如果 server 的管理員需要以下幾種行為,就會考慮使用轉址

  1. 轉移不同的 domain
  2. 容易發生打字錯誤的網址,需要重轉向
  3. 維持使用者體驗 (相同網址),但後端其實換了一個頁面

HTTP redirection, or URL redirection 是一種將某一個 domain name 或網址轉向另一個頁面的技術。那麼,如何利用簡單的方式達到 apache2 redirect 呢?

How to Redirect in Apache

1. Using the Redirect Directive

Debian 系統 Apache virtual host configuration files 的位置在 '/etc/apache2/sites-available'

Redirect permanent /username http://team.example.com/~username/
Redirect temp /username http://team.example.com/~username/
Redirect seeother /username http://team.example.com/~username/
Redirect gone /username

如果沒有特別指定,預設使用 temporary (302) status code,而且後面轉向的網址須要寫完整的

  • permanent : tells the client the resource has moved permanently. This returns a 301 HTTP status code.
  • temp : is the default behavior, and tells the client the resource has moved temporarily. This returns a 302 HTTP status code.
  • seeother : tells the user the requested resource has been replaced by another one. This returns a 303 HTTP status code.
  • gone : tells the user that the resource they are looking for has been removed permanently. When using this argument, you don’t need to specify a final URL. This returns a 410 HTTP status code.

編輯完成之後,請重新啟動 apache2 sudo systemctl restart apache2

2. RedirectMatch

Redirects can be made with regex patterns as well, using RedirectMatch:

RedirectMatch (.*)\.jpg$ http://static.example.com$1.jpg

results matching ""

    No results matching ""