国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php框架 > CakePHP > CakePHP URL重写

CakePHP URL重写

来源:程序员人生   发布时间:2014-09-09 07:51:08 阅读次数:4008次

URL 重写

Apache 和 mod_rewrite (.htaccess)

当 CakePHP 建立在以 mod_rewrite 为工作环境时(通常都会这么做) ,一些用户会努力去做一切能让系统更好运行的事情。

这里有一些事情你可以尝试让它能够正常运行。先看看你的 httpd.conf。 (请确保您编辑的是系统的 httpd.conf 而不是一个用户或站点特定的 httpd.conf。)

这些文件在不同的Apache发行版本之间有所不同。你可以先查看 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

在许多系统中在默认的性况下它都是被注释掉的,所以请删除前面的 "#" 符号。

进行更改后,重新启动Apache,以确保这些设置能够及时生效。

请确认你的 .htaccess 文件放置在正确的目录下,有些系统认为以 ”.“ 开头的文件是隐藏的不会进行复制。


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)。

这可以被添加到同一节中与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>

这些变化的细节将取决于你的设置,并且可以包含不相关的CakePHP的额外的东西。详情请参阅有关详细信息,Apache的联机文档。

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>

上述只会防止不正确的资源被发送到index.php文件,显示你的web服务器的404页面。

另外,你可以创建一个匹配的HTML404 页面,或通过增加一个 ErrorDocument 指令使用内置的CakePHP 404 默认为:

ErrorDocument 404 /404-not-found

关于 nginx 漂亮的 URL

nginx是一个使用较少的系统资源比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的规则到IIS使用CakePHP的URL重写。要做到这一点,请按照下列步骤操作:

  1. 使用 Microsoft’s Web Platform Installer 安装 URL Rewrite Module 2.0 或直接下载 (32-bit / 64-bit)。
  2. 创建一个名为web.config文件在CakePHP的根文件夹中。
  3. 使用记事本或任何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 重写规被正确的创建在IIS中, CakePHP的链接, CSS, JavaScipt, 和路由都能正确的工作。

lighttpd URL 重写

Lighttpd 不支持.htaccess的功能,这样你就可以删除所有.htaccess文件。在lighttpd的配置中,请确保您已激活“mod_rewrite”。添加如下面一行:

url.rewrite-if-not-file =( "^([^?]*)(?(.+))?$" => "/index.php?url=$1&$3" )

我不想或不能使用URL重写

如果你不想或不能使用URL重写您的网络服务器,请参阅核心配置。


生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生