dqvj51875 2014-06-04 16:47
浏览 29
已采纳

CakePHP URL重写与CakeRequest :: onlyAllow冲突

I am using $this->request->onlyAllow('post', 'delete'); so as to only allow the deletion of records that come from a POST request.

The problem is that I am using URL rewriting in my .htaccess file and it's changing the request from a POST to a GET

This is what my .htaccess file looks like:

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

I am using the postLink FormHelper to generate a delete button:

<?php 
    echo $this->Form->postLink(__('Delete'), 
    array('
        controller'=>'posts', 
        'action' => 'delete',), 
    null, 
    __('Are you sure you want to delete "%s?"', $attachment['Post']['name'])); 
?>

The problem is that the action for the form that is generated from the helper does not already have the trailing slash so the htaccess rule steps in and ads this which esentially changes this from a POST method to a GET

Action url that's generated: posts/delete/33579 Action url that's needed: posts/delete/33579/

I have tried adding a slash in the $this->Form->postLink() function however Cake encodes the slash and changes it to a %2F.

I am using CakePHPH 2.3.1

Any suggestions on how to fix this?

  • 写回答

1条回答 默认 最新

  • dougao2830 2014-06-04 16:53
    关注

    This is standard behaviour for a redirect. You have two options:

    • Fixing the url that is generated. This is the most sensible one, as you can probably imagine
    • Preventing the rule from matching if the request is a POST-request (or only let it match with a get request). You can do this with %{THE_REQUEST}

      RewriteCond %{THE_REQUEST} ^GET\ /
      RewriteRule ^(.+)([^/])$ $1$2/ [NE,R=301,L]
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧