我正在使用 cakephp v2.3.8 并且在路由方面遇到了一些困难。
旧网址是 www.domain.com/properties.aspx?prop=1999
我想将它路由到 id = 1999 的属性 Controller ,所以我的 url 看起来像:
www.domain.com/properties/view/1999
在我的 routes.php 中,我从 url 中删除了 .aspx,但在最后一点上遇到了困难。
Router::parseExtensions('htm','html','aspx');
这就是我的距离:
Router::connect('/properties', array('controller' => 'properties', 'action' => 'view', ??? '));
这是 PropertiesController.php 中的 View 函数
public function view($id = null) {
if (!$this->Property->exists($id)) {
throw new NotFoundException(__('Sorry, we couldn\'t find what you were looking for...'));
}
$options = array('conditions' => array('Property.' . $this->Property->primaryKey => $id));
$propData = $this->Property->find('first', $options);
$this->set('property', $propData);
}
这在网站内完美运行,但不适用于上述内容,它们将被重定向到 www.domain.com/properties
这是我在 public_html 文件夹中的 .htaccess 文件。
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
感谢您对此的调查。
文件结构:
.htaccess [1]
/app/.htaccess [2]
/lib/Cake/
/public_html/.htaccess [3]
/plugins/
/vendors/
[1]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
[2]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
[3]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
/public_html/index.php
if (!defined('ROOT')) {
define('ROOT', DS.'home'.DS. 'user');
}
/**
* The actual directory name for the "app".
*
*/
if (!defined('APP_DIR')) {
define('APP_DIR', 'app');
}
最佳答案
Nick 我认为这是 .htaccess 文件的问题
您已经在 public_html 中设置了 cakephp,但我认为您已经替换了包含如下代码的 cakephp 的 .htaccess 文件:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
将此 .htaccess 文件放在应用程序文件夹之外,即在 publich_html 文件夹中
关于cakephp路由从url检索参数重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18119026/