2

I am new to url re-writng and is currently facing problems with rewriting url for multiple applications in codeigniter. I have scanned the whole stackoverflow with some potential answers but it is still not solving my problems entirely

My directory at the root is as such

  • -application
  • --administrator
  • --frontend

What I am looking for is for my users to access my web site (frontend) with http://www.mydomain.com/ And my admin (backend) as http://www.mydomain.com/admin/

my current .htaccess is as such

<IfModule mod_rewrite.c>
    RewriteEngine On
# If the user types just "admin".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin$ administrator.php [L,QSA]

# If the user enter in any admin section, like "admin/section".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin\/(.*)$ administrator\.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]

    ErrorDocument 404 /index.php
</IfModule> 

code from CodeIgniter multi-application .htaccess problem

I can get what I desire at the front end with the above .htaccess, but I can't get it working on my back end. Basically, www.domain.com/admin gives a 404 page not found error.

Please advice me on the problem. Also, please guide me on where I should place this .htaccess file? at the root? inside the application folder? or inside the frontend and administrator folder respectively.

Many thanks.

Regards.

Community
  • 1
  • 1
Mark Seah
  • 532
  • 5
  • 18

2 Answers2

0

Why not something like this in your root directory.

RewriteEngine on
RewriteCond $1 !^(index\.php|update\.php|assets|admin|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]

This will basically ignore any directories such as "admin" to route to the frontend application. You will have to set your config.php and remove index.php from the controller.

Steven Lu
  • 2,150
  • 1
  • 24
  • 33
0

Found a way to work with this problem already. It is not a very smart way of doing it but it at least worked.

First, I created an 'admin' folder in the root, with a copy of the application's index.php Which effectively make my directory looking like this.

    -admin
    --.htaccess
    --index.php
    -application
    --frontend
    --administrator
    -index.php
    -.htaccess

For this solution you will need to have two .htaccess files in different location. For the .htaccess at the root, it should look like this.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|update\.php|administrator\.php|assets|admin|robots\.txt|favicon\.ico)
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

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

For the .htaccess in the admin folder

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

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

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


</IfModule>

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

While this works on localhost, it is noteworthy that it may not work on rented servers because the file system in your host may be different.

Mark Seah
  • 532
  • 5
  • 18