Simple way to get CakePHP to allow direct access to files and directories

Published by Walid Abdelaty on 26/04/09 14:54:59
Last edited on 06/05/09 18:20:54

Simple way to get CakePHP to allow direct access to files and directories.

CakePHP logoThe CakePHP framework as you know rewrites the browser URL to a friendly CakePHP implemented URL. This friendly URL reflects the policies that CakePHP follows which is the Model, View, and Controller (MVC). Briefly summarizing the MVC-URL correlation; Cake's input for a url is:
http://www.yoururl.com/model/action

Now to get out of this incapsulation for direct access to a download directory we must put icing on the Cake. This requires modifying .htaccess in the main directory where you extracted CakePHP. I used the linux command 'pico .htaccess' to open up an editor. The current .htaccess file should look similar or exactly like this:

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

And for the icing the updated .htaccess file should look like this:

<IfModule mod_rewrite.c>
    RewriteEngine On   
    RewriteBase /   
    RewriteCond %{REQUEST_URI} ^/downloads/(.*)$   
    RewriteRule ^.*$ - [L]
</IfModule>
# Begin CakePHP
<IfModule mod_rewrite.c>
    RewriteEngine on  
    RewriteRule    ^$ app/webroot/    [L]  
    RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

Thats all! Having put the new mod_rewrite block above the current CakePHP code block allows for overiding specific cases. You can change the directory in the example from downloads to any other directory you want to overide and allow direct access to files.

About the Author

Walid Abdelaty is one of our senior developers; nothing gets done without his involvement. Being a founder of Techlicity Ventures he’s been a key in the Technical and Business development of every project. Walid is known for sending out emails at 3AM regarding new features…or waking up at 3PM to reply to morning messages. A one of a kind never-the-less, and that's why we like him!

Bookmark and Share
Blog Widget by LinkWithin
blog comments powered by Disqus

Valid XHTML 1.0!