CakePHP URL重写与CakereRequest::onlyAllow冲突
我正在使用CakePHP URL重写与CakereRequest::onlyAllow冲突,php,.htaccess,cakephp,mod-rewrite,cakephp-2.3,Php,.htaccess,Cakephp,Mod Rewrite,Cakephp 2.3,我正在使用$this->request->onlyAllow('post','delete')以便仅允许删除来自POST请求的记录 问题是我在.htaccess文件中使用URL重写,它将请求从POST更改为GET 这就是我的.htaccess文件的外观: <IfModule mod_rewrite.c> Options -Indexes RewriteEngine On RewriteBase /example RewriteRule ^homes/
$this->request->onlyAllow('post','delete')
以便仅允许删除来自POST
请求的记录
问题是我在.htaccess
文件中使用URL重写,它将请求从POST
更改为GET
这就是我的.htaccess
文件的外观:
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteBase /example
RewriteRule ^homes/$ http://dev.example.com/ [R=301,L]
# if this is an existing folder/file then leave
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]
# if no trailing slash then redirect to url with trailing slash
RewriteRule ^/?(.+)([^/])$ $1$2/ [NE,R=301,L]
# internal rewrite to controller/dispatcher index.php
RewriteRule ^.*$ index.php [L,QSA]
</IfModule>
问题是,从帮助程序生成的表单的操作还没有尾部斜杠,因此htaccess规则会介入并添加ads,从而本质上将其从POST
方法更改为GET
生成的操作url:posts/delete/33579
需要的操作url:posts/delete/33579/
我曾尝试在$this->Form->postLink()
函数中添加斜杠,但Cake对斜杠进行编码并将其更改为%2F
我使用的是Cakephph2.3.1
关于如何修复此问题有何建议?这是重定向的标准行为。您有两个选择:
- 正在修复生成的url。正如你可能想象的那样,这是最明智的选择
- 如果请求是POST请求(或仅让其与get请求匹配),则阻止规则匹配。您可以使用
%{THE_REQUEST}
RewriteCond %{THE_REQUEST} ^GET\ / RewriteRule ^(.+)([^/])$ $1$2/ [NE,R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /
RewriteRule ^(.+)([^/])$ $1$2/ [NE,R=301,L]