How to Add a Trailing Slash to URLs in CakePHP

I’m building more web projects but found out that Content management System (CMS) that I normally use like WordPress, phpMyDirectory and others does not fulfill my needs. I’ve learned about PHP Framework such as CakePHP & decide to try it. With PHP Framework, I can build my own CMS. After many hours of working with it, I get it to works as per my needs.

My first CakePHP project completed but find out that many of the URLs generated does not have trailing slash. There is speculation that it is important for SEO. To do this you have to modify 2 files.

1. /app/View/Helper/AppHelper.php

Add the following codes in AppHelper.php. You can add more extension if you use it in order to avoid trailing slash inserted after the extension.

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

}

2. /app/webroot/.htaccess

Need to modify .htaccess file so that non trailing slashed URLs will be redirected to trailing slashed URLs. This is to avoid duplicate content, which is not good for SEO. This code is not limited to CakePHP, you can you for any other purpose.

<IfModule mod_rewrite.c>
 # Add trailing slash
 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
 RewriteRule ^(.*)$ http://www.yourwebsite.com/$1/ [L,R=301]
</IfModule>


10 comments… add one
  • this way could add a Trailing Slash to URLs. But CSS, JS, image files can not be loaded

    Reply
    • Make sure you add the code in “/app/View/Helper/AppHelper.php”. CSS, JS & Image should be able to load.

      Reply
      • Thanks for your response. I have added code to the path that you have mentioned above. But I my site still can not load CSS, JS, Image files. So that, i must to add new .htaccess file to CSS, JS and image folder to remove added splash (/) character, by this way my site can load those.
        I think this way is the temporary solution but not good. could you give me some advice or fix this bug for me?
        thanks so much !

        Reply
        • I’m using these codes in my CakePHP project and it works well. Which version of CakePHP you are using? I try on 2.4+ & 2.5+, no problem.

          Do you mean that you CSS become “www.example.com/style.css/” instead of “www.example.com/style.css”? If it is, then you may want to check which 1 of the code above causing the problem by trying the code one by one. I suspect the code in “/app/webroot/.htaccess” cause the problem. You may want to check that.

          Reply
          • thanks for your help,
            with my cakePHP i’m using, in /app/webroot/.htaccess file by default this file has line: RewriteRule ^ index.php [L]
            if i my /app/webroot/.htaccess file is:
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
            RewriteRule ^(.*)$ http://www.mywebsite.com/$1/ [L,R=301]
            then browser redirects to not found page. So that I have to add default line to this file, it become:
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
            RewriteRule ^(.*)$ http://www.mywebsite.com/$1/ [L,R=301]
            RewriteRule ^ index.php [L]
            it is why i have to add htaccess files to css, js,.. folder to remove splash character

  • You have to put the code in (2) above, on top of existing default code in /app/webroot/.htaccess

    In my case it become


    # Add trailing slash
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule ^(.*)$ http://www.example/$1/ [L,R=301]


    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    Reply
  • it’s cool, thank so much.
    with your solution i dont have to add htaccess file to css, js … folders.
    But i get new bug :(. hope you give me could you give me some advice?
    i have a form that allows users to login
    <form method="post" action=" ‘user_auth’, ‘action’ => ‘login’)) ?>”>
    by your way, this form could not submit to the action=login in controller=user_auth.
    If i add slash character to action, it become
    ‘user_auth’, ‘action’ => ‘login’) . “/” ) ?> it works like a charm
    thanks

    Reply
  • I’m not sure about your second problem but I think it must be related to code (1) above. Try add PHP extension as part of “rss|html|js|css|jpeg|jpg|gif|png|xml” & see whether it works or not.

    Reply
  • i have added to main.js to automatically add slash character to action for. It works fine.
    thanks so much for your help !
    have fun $_$

    Reply

Leave a Comment