Apache 和 mod_rewrite (.htaccess)
當 CakePHP 建立在以 mod_rewrite 為工作環(huán)境時(通常都會這么做) ,一些用戶會努力去做一切能讓系統(tǒng)更好運行的事情。
這里有一些事情你可以嘗試讓它能夠正常運行。先看看你的 httpd.conf。 (請確保您編輯的是系統(tǒng)的 httpd.conf 而不是一個用戶或站點特定的 httpd.conf。)
這些文件在不同的Apache發(fā)行版本之間有所不同。你可以先查看 http://wiki.apache.org/httpd/DistrosDefaultLayout 以進一步了解更多信息。
1. 確保一個.htaccess 是充許修改的,并且將DocumentRoot 的 AllowOverride
全部設置成 ALL。你應該看到下面類似的設置:
# Each directory to which Apache has access can be configured with respect
# to which services and features are allowed and/or disabled in that
# directory (and its subdirectories).
#
# First, we configure the "default" to be a very restrictive set of
# features.
#
<Directory />
Options FollowSymLinks
AllowOverride All
# Order deny,allow
# Deny from all
</Directory>
2. 請確保您正確加載了mod_rewrite。你應該會看到類似的設置:
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
在許多系統(tǒng)中在默認的性況下它都是被注釋掉的,所以請刪除前面的 "#" 符號。
進行更改后,重新啟動Apache,以確保這些設置能夠及時生效。
請確認你的
.htaccess 文件放置在正確的目錄下,有些系統(tǒng)認為以 ”.“ 開頭的文件是隱藏的不會進行復制。
3. 請確保您的CakePHP副本是從CakePHP網站或從GitHub中下載的,并正確的解壓,通過檢查是存在 .htaccess 文件。
CakePHP的根目(必須拷貝到您你的文檔目錄;一切都重定向到您的的CakePHP應用程序):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
CakePHP app 目錄(復制到應用程序的頂層目錄):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
CakePHP webroot 目錄(復制到應用程序的webroot目錄):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
如果您的CakePHP的網站的mod_rewrite 出現問題,你可能想嘗試修改虛擬主機設置。在Ubuntu上,編輯文件 /etc/apache2/sites-available/default(位置在
distribution-dependent)。 在這個文件中,確保 AllowOverride None 被修改為 AllowOverride All,
所以你必須按照下的方式進行修改:
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
在Mac OSX,另一種解決方案是使用工具 virtualhostx 做一個虛擬空間指向你的文件夾。
對于許多托管服務(如GoDaddy,1and1),您的Web服務器實際上在相應的目錄中使用了
mod_rewrite 應用。 如果您安裝了CakePHP的用戶目錄 (http://example.com/~username/cakephp/),
或者已經利用了mod_rewrite任何其他的URL結構, 你需要將 RewriteBase 指令語句添加到 .htaccess文件應用到CakePHP相應的目錄 (/.htaccess, /app/.htaccess, /app/webroot/.htaccess)。
這可以被添加到同一節(jié)中與RewriteEngine指令,所以舉例來說,你的Web根目錄 .htaccess 文件會是下面的樣子:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /path/to/cake/app
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
這些變化的細節(jié)將取決于你的設置,并且可以包含不相關的CakePHP的額外的東西。詳情請參閱有關詳細信息,Apache的聯(lián)機文檔。
4.(選修)要提高CakePHP生產安裝,你應該避免無效資產被解析了。 修改您的
webroot .htaccess 像:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /path/to/cake/app
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(app/webroot/)?(img|css|js)/(.*)$
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
上述只會防止不正確的資源被發(fā)送到index.php文件,顯示你的web服務器的404頁面。
另外,你可以創(chuàng)建一個匹配的HTML404 頁面,或通過增加一個
ErrorDocument 指令使用內置的CakePHP 404 默認為:
ErrorDocument 404 /404-not-found
關于 nginx 漂亮的 URL
nginx是一個使用較少的系統(tǒng)資源比Apache流行的服務器。 它的缺點是,它并沒有使用像Apache使用.htaccess文件,所以需要在站點上配置URL重寫。 這取決于你的設置,你將不得不修改這一點,但最起碼,你需要將運行的PHP作為一個FastCGI的實例。
server {
listen 80;
server_name www.example.com;
rewrite ^(.*) http://example.com$1 permanent;
}
server {
listen 80;
server_name example.com;
# root directive should be global
root /var/www/example.com/public/app/webroot/;
index index.php;
access_log /var/www/example.com/log/access.log;
error_log /var/www/example.com/log/error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
IIS7 URL重寫 (Windows 主機)
IIS7本身不支持.htaccess文件。雖然有附加軟件,可以添加這種支持,您還可以導入htaccess的規(guī)則到IIS使用CakePHP的URL重寫。要做到這一點,請按照下列步驟操作:
-
使用 Microsoft’s
Web Platform Installer 安裝 URL Rewrite
Module 2.0 或直接下載 (32-bit / 64-bit)。
-
創(chuàng)建一個名為web.config文件在CakePHP的根文件夾中。
-
使用記事本或任何XML編輯器,將以下代碼復制到新的web.config文件...
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite requests to test.php"
stopProcessing="true">
<match url="^test.php(.*)$" ignoreCase="false" />
<action type="Rewrite" url="app/webroot/test.php{R:1}" />
</rule>
<rule name="Exclude direct access to app/webroot/*"
stopProcessing="true">
<match url="^app/webroot/(.*)$" ignoreCase="false" />
<action type="None" />
</rule>
<rule name="Rewrite routed access to assets(img, css, files, js, favicon)"
stopProcessing="true">
<match url="^(img|css|files|js|favicon.ico)(.*)$" />
<action type="Rewrite" url="app/webroot/{R:1}{R:2}"
appendQueryString="false" />
</rule>
<rule name="Rewrite requested file/folder to index.php"
stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<action type="Rewrite" url="index.php"
appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
一量 web.config 重寫規(guī)被正確的創(chuàng)建在IIS中, CakePHP的鏈接, CSS, JavaScipt, 和路由都能正確的工作。
lighttpd URL 重寫
Lighttpd 不支持.htaccess的功能,這樣你就可以刪除所有.htaccess文件。在lighttpd的配置中,請確保您已激活“mod_rewrite”。添加如下面一行:
url.rewrite-if-not-file =(
"^([^?]*)(?(.+))?$" => "/index.php?url=$1&$3"
)
我不想或不能使用URL重寫
如果你不想或不能使用URL重寫您的網絡服務器,請參閱核心配置。