Question

I modified the paginator helper of CakePHP to printout the first page link without page, for example:

myexample.com/controller/action/id/page:1 It will be myexample.com/controller/action/id/ and any page else page:1 will be as usual.

At this point every thing is working fine. However, I noticed that myexample.com/controller/action/id/page:1 is still accessible as it is. I expect that it should be accessed without page:1. The following is the the route rule I use in config/routes.php

Router::connect('/action/:id/:page', array('controller' => 'controller', 'action' => 'action'), array('id' =>'[0-9]+', 'page' => 1, 'pass' => array('id')));

I need to know how could I eliminate the page:1 to be accessible or make auto-redirect to action/id becuse this situation causes SEO problems.

Notice: I use CakePHP 1.2.10

Any solution that includes .htaccess is welcomed too.

Was it helpful?

Solution 2

Finally I found the answer on this forum. I have to write the following line of code at the first line of the .htaccess found at app/webroot

RewriteRule ^(.*)page:1(.*)$ /$1 [R=301,L]

The final .htaccess will be:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)page:1(.*)$ /$1 [R=301,L]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

Important Notice:

If your application resident at sub-directory of your server's document root, you have to add it to the regarded line of code, otherwise, it will generate 404 not found. For example, supose that your aplication is found at /myapp which you are accessing it using http://localhost/myapp/, so the rewrite rule is going to be:

RewriteRule ^(.*)page:1(.*)$ /myapp/$1 [R=301,L]

OTHER TIPS

If we don't want the page:1 to wrongly route in 1.x/2.x we usually use another route to cover this:

Router::connect('/kuechen/alle', array('controller'=>'kitchens', 'action'=>'index_all'));
Router::connect('/kuechen/alle', array('controller'=>'kitchens', 'action'=>'index_all', 'page'=>1));
Router::connect('/kuechen/alle/*', array('controller'=>'kitchens', 'action'=>'index_all'));

Note the second one.

Then use canonical tag to make the /index/ without page:1 the canonical url. Done.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top