Search code examples
php.htaccesscodeigniterrelative-pathabsolute-path

.htaccess relative mod_rewrite path - simplify deployment of an application


Each time it's time to deploy a CodeIgniter application, I find myself struggling with changing the path in the .htaccess. Therefore; I'm looking for a proper, good working solution which works no matter of the location of the actual project.

Obstacle: My local testing enviroment isn't localhost/ but rather localhost/project. This won't, most likely, be the case in the live enviroment (more likely /). Hence, I'd prefer a solution which works anyway.

My current .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /project/index.php?/$1 [L]

    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /project/index.php?/$1 [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /project/index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /project/index.php
</IfModule>  

Using only index.php as a path won't work. It gives me an 404 not found error. Have tried a lot of different combinations, and would love to see your suggestions. Thanks!


Solution

  • Personally I dont like your denying of system and application folers in .htaccess.

    Per the "best practice on CI" guide found here (http://codeigniter.com/forums/viewthread/125687/) - you should move your application and system folders OUT of public_html, leaving only index.php. Then NO MATTER WHAT fancy stuff people try - they cant access your application & system folders from http.

    Then meanwhile - here is my .htaccess file that I use on ALL projects - with no modification required for ANY. It just "works" (for me).

    Options -Indexes
    Options +FollowSymLinks
    
    # Set the default file for indexes
    DirectoryIndex index.php
    
    <IfModule mod_rewrite.c> 
            # activate URL rewriting 
            RewriteEngine on  
    
            # do not rewrite links to the documentation, assets and public files 
            RewriteCond $1 !^(files|css|js|assets|uploads|captcha)
    
            # do not rewrite for php files in the document root, robots.txt                  
            RewriteCond $1 !^([^\..]+\.php|robots\.txt) 
    
            # but rewrite everything else   
            RewriteRule ^(.*)$ index.php/$1 [L]
     </IfModule>
    
     <IfModule !mod_rewrite.c>     
            # If we don't have mod_rewrite installed, all 404's
            # can be sent to index.php, and everything works as normal. 
            ErrorDocument 404 index.php 
      </IfModule>   
    

    Oh - and I use this for my multiple projects on localhost. i.e. localhost/project1 , localhost/project2

    just put a copy of the htaccess file in the root of each project. I DONT have this file in my root localhost