Add Trailing Slash to CakePHP
How to add a trailing slash to URLs in CakePHP using .htaccess and app helper.
I wanted to add a trailing slash to URLs in CakePHP. Why?
I also added 301 redirects to include the trailing slash on URL requests in my app level .htaccess
Download code
Whether or not it matters to add the trailing slash is speculative, however if you want to this is how I made it work.
- There is speculation that it matters for SEO to have the trailing slash. For instance, Wordpress still has a trailing slash on URLs, which that community is obsessive with SEO practices so I look to them as a sort of de-facto standard.
- I wanted CakePHP to do my bidding and learn in the process about URL handling
Helper Class:
Download code
<?php <?php
class AppHelper extends Helper {
function url($url = null, $full = false) {
$routerUrl = Router::url($url, $full);
if (!preg_match('/\\.(rss|html|js|css|jpeg|jpg|gif|png|xml?)$/', strtolower($routerUrl)) && substr($routerUrl, -1) != '/') {
$routerUrl .= '/';
}
return $routerUrl;
}
}
?>?>
You may need to add more file extensions if you use them.I also added 301 redirects to include the trailing slash on URL requests in my app level .htaccess
Download code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
#RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
You will need to change domain.com to your domain.Whether or not it matters to add the trailing slash is speculative, however if you want to this is how I made it work.
Comments
Comment
1 Cool
1) I think you should call the parent url() method after you've made changes to the URL.
2) You should probably add the some method overloading for the redirect() method.
I don't know how much this matters to some people, but if you're obsessive about SEO, I suppose this is one of those must-do solutions =)