0

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!

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Zar
  • 6,786
  • 8
  • 54
  • 76
  • 1
    Not an answer as such, but I prefer to set up virtual hosting on my local dev instance and create entries in the hosts file so they can be accessed with names. This means you can keep the directory structure identical to the way it will be in production and none of this is a problem. – DaveRandom Apr 13 '12 at 15:54
  • 1
    Also, you could just remove the `RewriteBase` and do everything with relative paths (couldn't you? I can't think of a reason it won't work but someone else may have one) – DaveRandom Apr 13 '12 at 15:56
  • 1
    @DaveRandom You're absolutely right, please post your comment as an answer so I can accept it. Removing `RewriteBase` and then pointing everything relatively to index.php made it work flawlessly. Thanks a lot! – Zar Apr 13 '12 at 19:52
  • I too set up a local vhost for testing. If I develop a site for e.g. `example.com` I create an entry in my dev-pc's host-file `127.0.0.1 example` and create a vhost for the `example` domain. – Gerben Apr 13 '12 at 20:39

1 Answers1

0

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

Laurence
  • 58,936
  • 21
  • 171
  • 212