在 Apache 啟用搜尋引擎網址友善化

From Joomla! Documentation

The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
This page is a translated version of the page Enabling Search Engine Friendly (SEF) URLs on Apache and the translation is 100% complete.
Other languages:
Deutsch • ‎English • ‎Nederlands • ‎español • ‎français • ‎português do Brasil • ‎中文(台灣)‎


Search engine friendly (SEF), human-readable or clean URLs are URLs that make sense to both humans and search engines because they explain the path to the particular page they point to. Since version 1.5, Joomla! is capable of creating and parsing URLs in any format, including SEF URLs. This does not depend on URL rewriting executed by the web server, so it works even if Joomla! runs a server other than Apache with the mod_rewrite module. The SEF URLs follow a certain fixed pattern, but the user can define a short descriptive text (alias) for each segment of the URL.

Internally, the local part of a SEF URL (the part after the domain name) is called a route. Creating and processing SEF URLs is therefore referred to as routing, and the relevant code is called a router.

本文描述如何在流行的開放源碼網站伺服器 Apache 中,設定 SEF URL。要在微軟IIS中設定 SEF URL 也是可以的,請參見 Enabling Search Engine Friendly (SEF) URLs on IIS.

從 Joomla! Joomla 1.6開始,預設支援基本的SEF urls ,在更早的版本中,您需要使用全站設定來啟用。假如您想要使用 Apache 的 mod_rewrite/URL rewriting,以下的教學也合用。

確定 .htaccess 是啟用的

檢查您的 Apache 設定檔案是否允許 .htaccess 覆寫。您必需要確定覆寫是允許的,否則 Joomla! 根目錄的 .htaccess 檔案會被略過,或是造成錯誤。在您的虛擬主機設定檔案或是主要主機檔案(httpd.conf) 的 <Directory> 部分,您必需有類似以下的設定來允許覆寫:

<Directory "/home/user/public_html">
  AllowOverride All
</Directory>

<Directory "/path/to/htdocs">
  AllowOverride All Options=[an option],[an option],...
</Directory>

如果您沒有進入網站設定檔案的權限,也有其他的方法可以測試.htaccess是否啟用。請參閱由The Apache Software Foundation提供的.htaccess tutorial 以獲得更多資訊。

逐步操作過程

這是逐步操作過程,請根據列點逐項執行,如果有某個步驟失敗,請 不要 繼續執行下步驟,除非錯誤的問題已經排除。

  1. 將位於Joomla! 頂層資料夾的檔案 "htaccess.txt" 重新命名為".htaccess"
  2. 本步驟可能不是必需的 使用文字編輯器來開啟 .htaccess,取消RewriteBase / 的註記狀態 (就是移除行前的 #)。假使 Joomla 安裝於自己的資料夾中,則您需要在斜線後面加上資料夾名稱。例如: RewriteBase /yourjoomlafolder.
  3. 登入後台管理區並開啟全站設定。
  4. 啟用 Use Apache mod_rewrite/URL rewriting 項目並 儲存。這個項目會利用 Apache 的 mod_rewrite 功能,來移除網址中的 "index.php" 部分。
    檢查網站是否正確運作。您的 URL 看起來會像是:
    http://www.example.com/the-­news/1­-latest-­news/1-­welcome-­to­-joomla
    如果有錯誤訊息,請參考 如何檢查mod rewrite在網站伺服器中是否啟用
    • 如果沒有啟用,而且您有存取檔案 apache/conf/httpd.conf 的權限,請開啟檔案並檢查 LoadModule rewrite_module modules/mod_rewrite.so 這一行有被取消註記。如果需要的話,就將其取消註記,必且重起 Apache 網站伺服器。
    • 如果 mod_rewrite 無法被啟用,,就讓選項維持關閉,保留重新命名過的 .htaccess 檔案是無妨的。
  5. 假如您覺得有必要,啟用 Add suffix to URLs儲存。這個項目會在網址後方加上 .html。有些觀點認為這可能是沒有需要,甚至是沒有意義的,搜尋引擎應該不會關心您的網址結尾是不是 .html
  6. 打開外掛管理員並啟用 系統 - SEF 外掛。這個外掛會讓您的 Joomla 文章支援 SEF 。這個是直接對 HTML 操作,並不需要特別的標記。