Welcome Guest, Not a member yet? Register   Sign In
Separate directories for web files and CI files
#1

Hey everybody, I'm learning CI coming from an extensive CakePHP background and so far I'm liking it a lot. From a 50,000-foot view, it seems like there's a lot less sludge to CI than there is to Cake. It's easier to get the common things done in CI than it is in Cake.

Except following common security practices by setting up separate directories for web files (images, scripts, stylesheets, uploads, etc.) and application files (CI core files, models, controllers, views, etc.). I've been all over Google, Stack Overflow, and these forums and I've found no solution.

Here's a closer look at my setup:
  • Apache web server running on Ubuntu Linux with its webroot set as /var/www/html and mod_rewrite enabled
  • Source code cloned from a Git repo to /var/www/html/PROJECT
  • An Apache vhost set up for the project with the docroot set to /var/www/html/PROJECT and .htaccess files enabled

Now, the project structure:
  • /app (application folder)
  • /lib (system folder)
  • /web (assets folder)
    • .htaccess
    • index.php
  • .htaccess
The .htaccess file in the webroot directs all requests to the /web directory. Here are its contents:


Code:
<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule ^$ web/ [L]
   RewriteRule (.*) web/$1 [L]
</IfModule>

The .htaccess file in the /web folder forwards requests to index.php. Here are its contents:

Code:
<IfModule mod_rewrite.c>
   Options +FollowSymLinks -Indexes
   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^(.*)$ index.php?/$1 [NC,L,QSA]
</IfModule>

So, here's my problem: I am accessing the application at http://localhost/PROJECT, but for some reason CI is picking the PROJECT term out of the URL and processing it as the default controller, which causes it to try to render Project/index, which doesn't exist. As a result, it throws a 404 error and writes this message to the log file:

Code:
DEBUG - 2015-05-08 23:32:33 --> UTF-8 Support Enabled
DEBUG - 2015-05-08 23:32:33 --> Global POST, GET and COOKIE data sanitized
ERROR - 2015-05-08 23:32:33 --> 404 Page Not Found: Project/index

Sorry for the wall of text, I've been struggling with for almost two days now. If anyone has a pearl of wisdom they could share, I'd be grateful.
Reply
#2

Try adding a rewritebase to your htaccess.

RewriteBase /PROJECT/

What do you have for your base_url in config? http://localhost/PROJECT/ ?
Reply
#3

(05-10-2015, 04:29 PM)CroNiX Wrote: Try adding a rewritebase to your htaccess.

RewriteBase /PROJECT/

What do you have for your base_url in config? http://localhost/PROJECT/ ?

I had a rewrite base in there at one point (it was just /PROJECT/) but it was causing infinite redirects whixh of course Apache wouldn't tolerate.


And yes, my base_url was http://localhost/PROJECT/, though I think I read somewhere that you should leave base url blank if you're using rewrite. Or it might have been index file? 

Thanks for the help. 
Reply
#4

If possible, you should configure your server to use the web directory as the root (DocumentRoot in Apache). Then the rewrite directives are used primarily to remove index.php from the URL and don't have to deal with the sub-directory, because anything above the web directory is not accessible to the outside world.

Bonfire has a similar structure, and just uses an index.php in the main directory to tell you the server needs to be configured to point to the "public" sub-directory instead of the root directory, and the .htaccess file in the main directory simply disables indexes.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB