Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove or add trailing slash from CakePHP 2.x on Apache using .htaccess

I am trying to enforce the removal or add of trailing slashes to my CakePHP 2.x app using the following inside the .htaccess located at /app/webroot.

#<IfModule mod_rewrite.c>
#    RewriteEngine On
#    RewriteCond %{REQUEST_FILENAME} !-d
#    RewriteCond %{REQUEST_FILENAME} !-f
#    RewriteRule ^ index.php [L]
#</IfModule>


<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.domain.com/$1 [L,R=301]
</IfModule>

or the following for add the trailing slash.

RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]

But I don't want to enforce the domain as it could be running locally or inside a subfolder. How do I fix this? All the docs I've read online seem to enforce the FULL url of the app.

In fact even if I use the FULL url it still doesn't work. Has anyone managed to get this working with CakePHP? Seems you MUST run it through the index.php file

To clarify I'm looking for a solution for ADDING and REMOVING trailing slashes in CakePHP without having to hardcode the url into the .htaccess file.

like image 963
Cameron Avatar asked Jan 21 '14 22:01

Cameron


1 Answers

This is usually not a complicated thing.

What about this ?

<IfModule mod_rewrite.c>
   RewriteEngine On

   # if this is an existing folder/file then leave
   RewriteCond %{REQUEST_FILENAME} -d [OR]
   RewriteCond %{REQUEST_FILENAME} -f
   RewriteRule . - [L]

   # if trailing slash then redirect to url without trailing slash
   RewriteRule ^/?(.+)/$ /$1 [R=301,L]

   # internal rewrite to controller/dispatcher index.php
   RewriteRule ^.*$ /index.php [L,QSA]
</IfModule>

If this one is not working, well this could be a problem on your side.
Did you already use mod_rewrite before ? Is it enabled ?


EDIT: if your website is not in root folder (DOCUMENT_ROOT) you have 2 options. You could put your htaccess in application folder or in root folder.

I suggest you to put your htaccess in your application folder (dedicated for your website only). Otherwise, the code would not be exactly the same. Here's the code in this case:

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /application/

   # if this is an existing folder/file then leave
   RewriteCond %{REQUEST_FILENAME} -d [OR]
   RewriteCond %{REQUEST_FILENAME} -f
   RewriteRule . - [L]

   # if trailing slash then redirect to url without trailing slash
   RewriteRule ^/?(.+)/$ $1 [R=301,L]

   # internal rewrite to controller/dispatcher index.php
   RewriteRule ^.*$ index.php [L,QSA]
</IfModule>

Your second question:

Also what would be the code for ENFORCING a trailing slash?

You could handle that with this code (only one line changed)

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /application/

   # if this is an existing folder/file then leave
   RewriteCond %{REQUEST_FILENAME} -d [OR]
   RewriteCond %{REQUEST_FILENAME} -f
   RewriteRule . - [L]

   # if no trailing slash then redirect to url with trailing slash
   RewriteRule ^/?(.+)([^/])$ $1$2/ [R=301,L]

   # internal rewrite to controller/dispatcher index.php
   RewriteRule ^.*$ index.php [L,QSA]
</IfModule>
like image 173
Justin Iurman Avatar answered Oct 22 '22 17:10

Justin Iurman