EllisLab text mark
Advanced Search
1 of 2
1
   
.htaccess for admin panel
Posted: 05 October 2012 08:26 AM   [ Ignore ]
Joined: 2010-07-20
74 posts

Hi everyone,

I have 2 applications for my frontend/backend but I can’t find the right .htaccess in order to remove the index.php and admin.php, and have this behavior :

Frontend :
http://www.domain.com/controller/function/

Backend :
http://www.domain.com/admin/controller/function

I’ve searched a long time for a bit of code resolving my problem, but I haven’t ever seen someone with my folder structure :

/admin/
/
site/
/
system/
/
index.php (pointing to /site)
/
admin.php (pointing to /admin)
/.
htaccess 

I easily managed to remove index.php from my URLs, but the admin.php…

Can someone please help me? Thanks wink

 
Posted: 05 October 2012 09:01 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2011-02-23
882 posts

Should be something like this

Options +FollowSymLinks
Options 
+Indexes
RewriteEngine On
RewriteBase 
/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond 
%{REQUEST_FILENAME} !-d

RewriteCond 
$!^(admin\.php)
RewriteRule ^admin(.*)$ /admin.php/$1 [L]

RewriteCond 
$!^(index\.php)
RewriteRule ^(.*)$ /index.php/$2 [L] 

taken from the default .htaccess. But don’t trust me too much since I’m not that proficient with RewriteConds and RewriteRules (but with RegExs I’m going well, just not sure how the RewriteRule “needs it”)

Anyway this should serve as a good starting point for others wink

PS: Have you thought of just putting all your admin-related controllers into an “admin”-subfolder in your controllers directory and securing it via session? Would make your life soooo much easier wink

 Signature 

ignited Community Framework (WiP)  |  Read the User’s Guide. It won’t bite.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

CI example .htaccess

 
Posted: 15 October 2012 05:04 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2010-07-20
74 posts

Any other solution ?

I guess i have to set in my admin/config/config.php :

$config['index_page''admin'

I tried playing with PhilTem answer, but could not reach a good result, still having “404 Page Not Found”

Here is my .htaccess

Options +FollowSymlinks -MultiViews -Indexes

<IfModule mod_rewrite.c>
 
 
RewriteEngine on
 RewriteBase 
/muki
 
 
# If the user types "index.php" or "admin.php".
 
RewriteCond $!^(index\.php|admin\.php|images|robots\.txt)
 
 
# If the user types just "admin".
 
RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond 
%{REQUEST_FILENAME} !-d
 RewriteRule 
^adminadmin\.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]
</IfModule>

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

PS : I’d like to keep my folder structure as it is now, but if it is impossible to redirect correctly, then surely…

Thanks for your help smile

 
Posted: 15 October 2012 02:26 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2009-01-20
59 posts

Hi

for admin/config/config.php

1. $config[‘index_page’] = ‘’;
You don’t have to use admin here. Just use URI routing instead as described here http://ellislab.com/codeigniter/user-guide/general/routing.html

2. $config[‘uri_protocol’] = ‘REQUEST_URI’;

3. I have following .htaccess in my application:

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

 
Posted: 16 October 2012 03:46 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

If you’re using one of the newer versions of CI, there is an .htaccess file inside the application directory that denies any direct requests for it. Assuming you’ve copied the application folder and renamed it “admin”, that .htaccess folder is still there. When you access a URL that begins with “admin”, it will find the physical directory, see that there is another .htaccess file inside of it, and automatically give it priority, effectively denying all requests. There is also the problem with the directory check in the home .htaccess - since it’s a valid physical directory, the admin rewrite won’t be initiated.

1. Don’t name your admin application folder “admin”. Name it something else. Update your admin.php “index” file appropriately.

2. RewriteCond lines are only valid for a single RewriteRule call. Once you call a RewriteRule, you need to duplicate any of the same RewriteCond, or they will not be available.

3. In your admin’s config.php file, you do not HAVE to use the index_page setting. However, if you add “admin” to it, CI’s convenient URL helpers will automatically prepend that segment, making creating links a little easier. Then you can do things like this:

echo site_url('viewposts'); // Generates: http://mysite.com/admin/viewposts 

4. Here is a more suitable .htaccess for you to at least start with. Add to it where necessary:

Options +FollowSymlinks -MultiViews -Indexes

<IfModule mod_rewrite.c
 
RewriteEngine on
 RewriteBase 
/muki
 
 
# Admin URLs
 # RewriteCond's are not necessary here if there will be no
 # static assets (images/css/js) whose URLs start with "admin"
 
RewriteRule ^admin(/.*)?$ admin.php/$1 [L,QSA]
 
 
# Public website
 # The first two rules say "If this path is not an existing file or directory"
 # The third conditional is good for preventing rewrites for directories
 # commonly accessed for static assets. It will prevent unnecessary calls to
 # your application if there are any 404 errors in those directories.
 
RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond 
%{REQUEST_FILENAME} !-d
 RewriteCond 
$!^(images|css|js)
 
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>

<
IfModule !mod_rewrite.c>
    
ErrorDocument 404 index.php
</IfModule
 
Posted: 16 October 2012 04:08 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2010-07-20
74 posts

Thanks for your answers guys.

I’m using one of the latest CI (don’t know which one but it’s a 2.x version), and I noticed the .htaccess inside applications folder denying request. I just deleted them… Is this wrong ? Or should just change “deny” to “allow” ? But the .htaccess won’t have any sense there after I guess !

Aken - 16 October 2012 03:46 AM

1. Don’t name your admin application folder “admin”. Name it something else. Update your admin.php “index” file appropriately.

Why that ? In terms of security because everybody knows the /admin trick ?

I tried your sample of .htaccess :
Frontend works, but backend sends me back to the front without the /admin prefix.

PS : I hate .htaccess files…


I played with config/routes file as mentionned by NiconPhantom.
Well It did pretty good, if I set in my config/config.php

$config['index_page''admin'

And in my config/routes.php

$route['admin/(:any)'"/$1"

All is working great, backend almost no problem for URLs like /admin/controller/function/params/ except for the login controller by default, that is not accessible when I just type /admin. It says “403 : Directory access is forbidden.” from the index.html.
How can I fix that ?

 
Posted: 16 October 2012 04:38 AM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

Leave the .htaccess in your application and system directories as they are - they’re there for a reason.

And read my response and try what I recommended - you’re not doing any of it and asking me why there are still problems.

 
Posted: 16 October 2012 04:58 AM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Joined: 2009-01-20
59 posts

Hi Noslen1,

Could you please add following line to routes.php:

$route[‘admin’] = “admin”; or $route[‘admin’] = “admin/admin”; according to hierarchy

$route[‘admin’] is basically URI and “admin” = desired controller

Hope that helps,

Alex

 
Posted: 16 October 2012 05:02 AM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts
NiconPhantom - 16 October 2012 04:58 AM

Hi Noslen1,

Could you please add following line to routes.php:

$route[‘admin’] = “admin”; or $route[‘admin’] = “admin/admin”; according to hierarchy

$route[‘admin’] is basically URI and “admin” = desired controller

Hope that helps,

Alex

You’re not helping. He’s trying to route all admin requests to an entirely different application, including folder and main .php file - routes are not applicable to this.

 
Posted: 16 October 2012 06:05 AM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Joined: 2010-07-20
74 posts

Ok Aken, so :

- I kept .htaccess in my applications folder.
- I renamed my ‘/admin’ application folder as ‘/blob’, renamed my ‘admin.php’ as ‘blob.php’ as well, modified it to $application_folder = ‘blob’;
- Deleted my added routes
- I change the config as $config[‘index_page’] = ‘’
- I took your .htaccess sample, and changed where you type ‘admin’ and replaced with ‘blob’ :

RewriteRule ^blob(/.*)?$ blob.php/$1 [L,QSA] 

I think i’ve followed your recommandations now, but it does not solve my issue.
Typing URL as localhost/muki/blob throws me a “403 Forbidden You don’t have permission to access /muki/blob/ on this server.”

 
Posted: 16 October 2012 07:10 AM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Joined: 2009-01-20
59 posts

Where your application is hosted? Is it your own server on WAMP/ LAMP?

Do you have any .htaccess inside blob/ ? If you will temporary remove it, will you see 403?

Sounds like: http://www.cyberciti.biz/faq/apache-403-forbidden-error-and-solution/

 
Posted: 16 October 2012 08:10 AM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Joined: 2010-07-20
74 posts

Yes CI is installed on my localhost on WAMP.
If I remove /blob/.htaccess, I don’t get a 403 but 404 error

 
Posted: 16 October 2012 09:12 AM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Joined: 2009-01-20
59 posts

what if you will try /blob/index.php or any other file directly, will it still trigger 404?

Make sure you have mod_rewrite enabled for Apache -> WAMP, otherwise it will give 404 for .htaccess routes

I found following picture which might be useful: http://docs.tomatocms.com/images/4/40/Wamp_rewrite_module.jpg

 
Posted: 16 October 2012 09:24 AM   [ Ignore ]   [ # 13 ]   [ Rating: 0 ]
Joined: 2010-07-20
74 posts

Of course my mod_rewrite is enabled in Apache.

I don’t have index.php in my /blob directory.
My folders structure looks like :

/blob/
    /
cache/
    /
config/
    /
controllers/
    /... 
//other classical directories from application folder
    
/.htaccess
    
/index.html
/site/
    /
cache/
    /
config/
    /
controllers/
    /... 
//other classical directories from application folder
    
/.htaccess
    
/index.html
/www/
    /
admin/
        /
css/
        /
img/
        /
js/
    /
site/
        /
css/
        /
img/
        /
js/
/
system/
    /
core/
    /
database/
    /... 
//other classical directories from system folder
/.htaccess
/index.php // routing to /site/
/blob.php // routing to /blob/ 

If I try to access /blob.php, I successfully arrive on my admin login page (the default login controller), then I submit the form and it’s trying to send me to localhost/muki/login, instead of localhost/muki/blob/login or localhost/muki/blob.php/login

 
Posted: 16 October 2012 09:31 AM   [ Ignore ]   [ # 14 ]   [ Rating: 0 ]
Joined: 2009-01-20
59 posts

Do you have HMVC like https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home? If no, then it will be easier for you to have it to separate different apps and use routing instead of .htaccess

 
Posted: 16 October 2012 09:35 AM   [ Ignore ]   [ # 15 ]   [ Rating: 0 ]
Joined: 2010-07-20
74 posts

Nope I don’t have HMVC. Should I get into it or should I rearrange my folder structure ?
What would be the most typical and easiest way to structure a backend application besides a frontend in CI ?

 
1 of 2
1