3
votes

I am using cakephp v2.3.8 and struggle a bit with the routing.

The old url is www.domain.com/properties.aspx?prop=1999
I want to route this to the properties controller with id = 1999 so my url will look like: www.domain.com/properties/view/1999

In my routes.php i strip the .aspx out of the url but struggle with this last bit.

Router::parseExtensions('htm','html','aspx');

This is how close I am:

Router::connect('/properties', array('controller' => 'properties', 'action' => 'view', ??? '));

This is the view function in the PropertiesController.php

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);
}

This works perfect within the website, but not for those as mentioned, they are redirected to www.domain.com/properties

This is my .htaccess file in the public_html folder.

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]

Thanks for looking into this.

File structure:

.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');
}  
3
exactly which type of url you want please can you give me any demo urlEr.KT
The new url should be: www.domain.com/properties/view/1999Nick
Nick want to confirm: you are passing URL www.domain.com/properties.aspx?prop=1999 and its redirecting to www.domain.com/properties, right?Er.KT
Nick one more question that in public_html folder you have setup cakephp in any folder or in public_html folder itself?Er.KT
Hi, yes that is correct any url from the internet with www.domain.com/properties.aspx?prop=1999 goes to www.domain.com/properties. CakePHP is installed above public_html I suppose this can give confusion, so here is the real path /home/user/app thus above /home/user/public_html I hope this is clear as some prefer to say below and others above :-) /app and /public_html are at same level in tree. Everything works fine throughout the website, it is only this url what gives a lot of problems and my Google webmaster tools is full with errors.Nick

3 Answers

2
votes

Nick I think its the problem due to .htaccess file

You have setup cakephp in public_html but I think you have replaced .htaccess file of cakephp which contains code like:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

place this .htaccess file at out side the app folder,means in publich_html folder

1
votes

You can use something like this:

Router::connect('properties/view/:id',
array('controller' => 'properties', 'action' => 'view'),
  array(
      'pass' => array('id')
  )
);

This 'id' will then be passed to 'view' function inside 'properties' controller as a parameter

0
votes

Go for

Router::connect('/Properties/*',array('controller' => 'Properties', 'action' => 'view'),array('id' => '[0-9]+'));