Public folder in CodeIgniter

October 31st, 2010 by ScottK | No Comments | Filed in CodeIgniter

CodeIgniter is fairly logically laid out, but lacks a public asset folder. You can of course create a public folder in your web root for these and did so for many years, but when it comes to things as robots.txt and SE verification files they needed to stay in the web root. I’ll so you how with htaccess modification you to can keep all your static assets in the public folder and nothing but core files in the web root.

What I mean by a “public” folder is nothing more than a folder called public to hold all your static files. These include: css; javascript; images; html pages; etc. Basically anything that CodeIgniter isn’t generating. Regrettably the url to retreive these is http:///public/
. That’s totally fine to when retrieving images, stylesheets, images etc, but doesn’t work so well when SE call http:///robots.txt or even http:///sitemap.xml. CodeIgniter will 404 for these types of files.

Of course you could place these in the web root but then you have to manage files both there and in the public folder. Myself I would rather keep like files together so with a few changes to the htaccess file you can serve http:///robots.txt from the public folder with no problem.

In your htaccess file add this below the RewriteBase / statement:

RewriteCond %{REQUEST_URI} !public/(.*)\.
RewriteRule ^(.+)\.htm$|^(.+)\.html$|^(.+)\.txt$|^(.+)\.xml$|^(.+)\.xml.gz$ public/$0 [NC]

That command statement instructs Apache to disregard any request that doesn’t include the public folder. If the request isn’t looking for assets in the public folder and the extensions includes .htm, .html, .txt, .xml, .xml.gz then redirect to the public folder.

So now you can have the robots.txt file placed into the public folder along with all your other static files, yet served up from a request of the root url.

Tags: ,



Share Your Thoughts