啟用搜尋引擎網址友善化

From Joomla! Documentation

This page is a translated version of the page Enabling Search Engine Friendly (SEF) URLs and the translation is 57% complete.

Other languages:
English • ‎中文(台灣)‎

搜尋引擎網址友善化 (SEF), 人類可讀 或稱為 乾淨 URL 是指 URL 能解釋特定頁面的內容,對於人類或是搜尋引擎而言是有意義的。從Joomla 1.5起,Joomla! 就可以建立並解析任何格式的 URLs ,也包含 SEF URL。這並不需要依賴網站伺服器的 URL rewriting 是啟用的,因此即使 Joomla! 執行於 Apache + mod_rewrite 模組以外的網站伺服器都能用。SEF URL 會依據特定的固定格式,然而用戶可以為每一個部分來定義 short descriptive text (別名)

在內部,SEF URL 的本機部分 (在域名之後的部份)稱為路由。因此建立以及執行 SEF URL也是名為 路由,相關的程式碼也稱為路由

這個教學是關於啟用搜尋引擎網址友善化。假如您使用共享的寄存空間,那可能會是 Apache。假如您不確定的話,請洽詢您的服務供應商。

本文只有涵蓋較為流行的網站伺服器,參見 Enabling Search Engine Friendly (SEF) URLs on IIS 以及 Category:Search Engine Friendly URLs/zh-tw 以查看更多文章。

Apache

確定 .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 操作,並不需要特別的標記。


Hiawatha

Use the following UrlToolkit rule to enable clean URLs on the Hiawatha webserver:

UrlToolkit {
    ToolkitID = joomla
    Match base64_encode.*\(.*\) DenyAccess
    Match (<|%3C).*script.*(>|%3E) DenyAccess
    Match GLOBALS(=|\[|\%[0-9A-Z]{0,2}) DenyAccess
    Match _REQUEST(=|\[|\%[0-9A-Z]{0,2}) DenyAccess
    RequestURI exists Return
    Match /index.php Return
    Match ^/component/ Skip 2
    Match ^(/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ Skip 1
    Skip 1
    Match .* Rewrite /index.php
}

Enable an UrlToolkit rule for a virtual host via the UseToolkit setting:

VirtualHost {
    ...
    UseToolkit = joomla
}

IIS 7

If you have a server running IIS 7 and PHP, you can take advantage of IIS's own internal URL rewriting by using a web.config file similar to the one listed below.

You can create the file yourself or use the GUI in the IIS7 Manager. You can import .htaccess rules using the GUI/wizard.

This functionality depends on the presence of the IIS URL Rewrite Module, which does not come with Windows. It is a free download and a Microsoft product.

GUI

If the IIS URL Rewrite module is installed, your website manager will have a tool for "URL Rewrite", visible in the IIS Manager's view of your site's configurable IIS modules. The interface is largely self-explanatory. Regular expressions, wildcards or exact matches are all supported.

In the Joomla config turn on both the SEF and Apache mod_rewrite, next create a rule under IIS URL Rewrite:

Pattern field: ^([^/]+)/?$
Ignore case ON
Action type: Rewrite
Rewrite URL: index.php/

web.config

This has been tested on Joomla 1.5 with IIS 7 on Windows Server 2008 with no problems so far. For more information on converting .htaccess to web.config, check out http://learn.iis.net/page.aspx/557/translate-htaccess-content-to-iis-webconfig/

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="Common Exploit Blocking" stopProcessing="true">
                    <match url="^(.*)$" />
                    <conditions logicalGrouping="MatchAny">
                        <add input="{QUERY_STRING}" pattern="mosConfig_[a-zA-Z_]{1,21}(=|\%3D)" />
                        <add input="{QUERY_STRING}" pattern="base64_encode.*\(.*\)" />
                        <add input="{QUERY_STRING}" pattern="(\&lt;|%3C).*script.*(\>|%3E)" />
                        <add input="{QUERY_STRING}" pattern="GLOBALS(=|\[|\%[0-9A-Z]{0,2})" />
                        <add input="{QUERY_STRING}" pattern="_REQUEST(=|\[|\%[0-9A-Z]{0,2})" />
                    </conditions>
                    <action type="Redirect" url="index.php" appendQueryString="false" redirectType="SeeOther" />
                </rule>
                <rule name="Joomla Search Rule" stopProcessing="true">
                    <match url="(.*)" ignoreCase="true" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{URL}" pattern="^/search.php" ignoreCase="true" />
                    </conditions>
                    <action type="Rewrite" url="/index.php?option=com_content&amp;view=article&amp;id=4" />
                </rule>
                <rule name="Joomla Main Rewrite Rule" stopProcessing="true">
                    <match url="(.*)" ignoreCase="true" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{URL}" pattern="(/[^.]*|\.(php|html?|feed|pdf|raw))$" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php/" />
                </rule>
            </rules>
        </rewrite>
        <caching>
            <profiles>
                <add extension=".php" policy="DisableCache" kernelCachePolicy="DisableCache" />
            </profiles>
        </caching>
    </system.webServer>
</configuration>


Nginx

  • Please add the next code to your server (vhost) configuration in the nginx.conf file:
   # Support Clean (aka Search Engine Friendly) URLs
   location / {
      try_files $uri $uri/ /index.php?$args;
   }
  • If the above does not work, add the next code to your server configuration in the nginx.conf file: (This worked with nginx 1.4.6 on Ubuntu.)
server {
  ....
  location / {
     expires 1d;

     # Enable joomla SEF URL's inside Nginx
     try_files $uri $uri/ /index.php?$args;
  }
  ....
}
  • Log on to your Back-end and open the Global Configuration.
  • Enable the Search Engine Friendly URLs option and Save. This option converts the URLs from the native Joomla! format to the SEF format.
    Verify that your site works correctly. Your URLs should now look like http://www.example.com/index.php/the-­news/1-­latest­-news/1­-welcome­-to­-joomla.
    If your site does not work correctly, please see Why does your site get messed up when you turn on SEF (Search Engine Friendly URLs)?
  • Enable the Use Apache mod_rewrite/URL rewriting option and Save. This option uses the Apache mod_rewrite function to eliminate the "index.php" portion of the URL.
    Verify that your site works correctly. Your URLs should now look like http://www.example.com/the-­news/1­-latest-­news/1-­welcome-­to­-joomla.
    If this option causes errors, please see How to check if mod rewrite is enabled on your server.
    If it is not enabled and you have access to the file apache/conf/httpd.conf, open that file and verify that the line LoadModule rewrite_module modules/mod_rewrite.so is uncommented. If necessary, uncomment the line and restart the Apache web server.
    If mod_rewrite cannot be enabled, leave this option off. It does not matter if you leave the .htaccess file renamed.
  • If you think this is necessary, enable Add suffix to URLs and Save. This option adds .html to the end of URLs. There are different opinions on whether this is necessary or even useful. Search engines do not seem to care if your URLs end in .html or not.
  • Open the Plugin Manager and enable the System - SEF plugin. This plugin adds SEF support to links in your Joomla articles. It operates directly on the HTML and does not require a special tag.