views:

61

answers:

1

Hello.

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!

+2  A: 

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.

Raoul Duke
Thanks @Raoul, this really helped! I got it to work now, I don't know if it is redundant or not, but it work! I updated the main post with the solution. If someone has a more correct solution that works the same way I would be grateful.
Philipe