2014-06-10 10:17:32  浏览:1305

cakephp

Cakephp部署到window服务器,访问会报错,解决办法有两个

1:不使用伪静态

在app/config/core.php中有如下注释(或类似)

/**
* To configure CakePHP *not* to use mod_rewrite and to
* use CakePHP pretty URLs, remove these .htaccess
* files:
*
* /.htaccess
* /app/.htaccess
* /app/webroot/.htaccess
*
* And uncomment the App.baseUrl below:
*/
//Configure::write('App.baseUrl', env('SCRIPT_NAME'));

按照所说,将 //Configure::write('App.baseUrl', env('SCRIPT_NAME')); 的注释删除掉,并删除.htaccess文件

这样配置只有,访问地址就变成了 http://www.choujone.com/index.php/page/index

虽然可以访问到内容,但是看起来不很美观

2:使用伪静态(首先确定服务器已经支持rewrite)

网上有很多版本,由于我使用的是window版本的虚拟空间,那些版本似乎都不适用,下面是我自己整理的。

首先在app\webroot\index.php 的21行增加如下代码,意思是强制通过get方式设置REQUEST_URI

/* Begin IIS MOD_REWRITE Code */
$_SERVER['REQUEST_URI'] = $_GET['REQUEST_URI'];
unset($_GET['REQUEST_URI']);
/* End IIS MOD_REWRITE Code */

第二步,删除app/webroot/下的.htaccess

第三步,修改根目录下面的.htaccess文件

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?$ /index.php [L]
RewriteRule ^(json|responsivewebdesign|uploadcrop|zhongzi|hongbao)?$ /app/webroot/$1/index.html [L]
RewriteRule ^(json|responsivewebdesign|uploadcrop|zhongzi|hongbao)(.*)$ /app/webroot/$1$2 [L]
RewriteRule ^(img|css|files|js)(.*)$ /app/webroot/$1$2 [L]
RewriteRule ^(.*)$ /index.php?REQUEST_URI=$1 [L]
</IfModule>

说明:

<IfModule mod_rewrite.c>

RewriteEngine on

 //直接域名访问
RewriteRule ^/?$ /index.php [L]  

 //访问app/webroot/下的文件目录静态文件, 例如 http://www.choujone.com/json,需要注意index.html中,需要加上 <base href="/xxx/"/>,而且在引用js/css的时候也需要注意带上xxx这个路径

//如果没有这种需求,可以删除
RewriteRule ^(json|responsivewebdesign|uploadcrop|zhongzi|hongbao)?$ /app/webroot/$1/index.html [L]  

 //加载app/webroot/下这几个目录里面的文件,例如css、js、img等//如果没有这种需求,可以删除
RewriteRule ^(json|responsivewebdesign|uploadcrop|zhongzi|hongbao)(.*)$ /app/webroot/$1$2 [L]   

//加载app/webroot/下这几个目录里面的文件,例如css、js、img等
RewriteRule ^(img|css|files|js)(.*)$ /app/webroot/$1$2 [L]

//其他所有的请求,都执行index.php
RewriteRule ^(.*)$ /index.php?REQUEST_URI=$1 [L]
</IfModule>

配置完毕

=-===========其他解决方案========--=-=-=-=-=-=

[转自]http://bakery.cakephp.org/articles/filippo.toso/2008/01/29/cakephp-on-iis

1. First of all download the URL Rewrite Filter for IIS (it's free and open source). Since it seems the official web site (http://www.iismods.com/) has been closed, I have managed to publish a copy of the filter on my company web site. You can download it from the following URL: 

http://www.creativepark.it/downloads/iismod_rewrite.zip 
2. The installation is pretty straight forward.

3. Edit the mod_rewrite.ini as follows:

Debug 0 
Reload 5000 
RewriteRule ^/$    /index.php?REQUEST_URI=index.php [L] 
RewriteRule ^(.*)$ /index.php?REQUEST_URI=$1        [L]
4. Close the IIS Management Console and restart the web server. You can use the included restart_iis.bat batch script, the IIS Management Console or the Services Management Console.

5. Add the following code at the beginning of the index.php file of CakePHP:

/* Begin IIS MOD_REWRITE Code */ 
$_SERVER['REQUEST_URI'] = $_GET['REQUEST_URI']; 
unset($_GET['REQUEST_URI']); 
/* End IIS MOD_REWRITE Code */
If you have correctly followed the above steps you should be able to run your CakePHP web applications under IIS.

Support for Virtual Folders 
Under client Windows Operating Systems (i.e. Windows 2000/XP Pro), IIS allows to run only one web site. If you need to test multiple web applications, you can use the Virtual Folders. For instance, if you want to put a CakePHP web application into a /cakephp Virtual Folders, you have to edit the mod_rewrite.ini file as follows:

Debug 0 
Reload 5000 
RewriteCond HTTP_HOST localhost 
RewriteRule ^/cakephp/?$    /cakephp/index.php?REQUEST_URI=index.php [L] 
RewriteRule ^(/cakephp/.*)$ /cakephp/index.php?REQUEST_URI=$1 [L]

You can duplicate the two RewriteRule rows multiple time to support multiple web applications, just remeber to replace che /cakephp string with the right Virtual Folder path.

P.S. If you have a copy of iismod.com Mod Rewrite and Mod Auth sources, please send it to me. I have downloaded them a while ago but I can't find them anymore.

[转自]http://bakery.cakephp.org/articles/filippo.toso/2008/01/29/cakephp-on-iis

如果这样不能达到你想要的效果,可以看看这篇文章的评论,有很多种配置方式,多试试总有一种适合你

 

返回首页