CodeIgniter multi-application .htaccess problem

I have a problem with CodeIgniter .htaccess file and hope that somebody can help me!
I don’t know regular expressions or how to use the .htaccess, I’m very new with this things!

My Problem is:

I have this structure of files (multi application):

- application
    - admin
    - site
- system
- index.php
- admin.php

And this .htaccess (that works fine on localhost!)

Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

What I want to do is:

Access my web site (frontend) with http://www.mydomain.com/
And my admin (backend) as http://www.mydomain.com/admin/

I don’t know why this works fine on my local server.

I hope someone can help me.

UPDATE

I got it to work using this:

RewriteEngine on

# If the user types "index.php" or "admin.php".
RewriteCond $1 !^(index\.php|admin\.php|images|robots\.txt)

# If the user types just "admin".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin$ admin\.php [L,QSA]

# If the user enter in any admin section, like "admin/section".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin\/(.*)$ admin\.php/$1 [L,QSA]

# If the user types any site section, like "site/section".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index\.php/$1 [L,QSA]

This way I can access http://www.mydomain.com/admin or http://www.mydomain.com/admin/any-section (that points to “admin.php” on the root directory)

And I can access http://www.mydomain.com/ or http://www.mydomain.com/any-section (that points to “index.php” on the root directory)

Thanks for the help!

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

First of all this is redundant:

RewriteCond $1 !^(index\.php|images|robots\.txt)

because your other conditions already take care of existing files and directories so I’d suggest to remove it.

Your rewrite rule is wrong if you want /admin to rewrite to admin.php, with your current rule it will result in /index.php/admin. You didn’t mention if that’s the problem, if it is do this:

RewriteRule ^/admin$ admin.php [L,QSA]
RewriteRule ^(.*) index.php/$1 [L,QSA]

Also you can debug the rewriting using RewriteLog: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritelog

If that doesn’t fix it please describe the current behaviour and what you expect, otherwise it’s difficult to help.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply