Change default index.php when using admin route

I'm currently on a project which gave me a small headache after trying to find a solution for a tiny problem I've bumped onto.

At the moment, my application loads my index.php file on every single page request it gets. Inside my index, the application fires-up some basic stuff, sets some variables etc. Well, the thing I'm currently looking for is that I would like to let my server load a different file (for example admin.php) as default. But, this only needs to happen when the url sub-directory is set as

So the server needs to handle every little thing exactly the same, but when the route is set as /admin/, its needs to autoload the admin.php instead of the default index.php.

Currently my .htaccess file looks like this

# mod-rewrite engine RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L] 

EDIT: To clear things up and visualize it a bit more:

  • -> index.php
  • -> admin.php

  • -> admin.php - -> index.php

Inside these two file I load a new Zend_Application (ZF1)

2

3 Answers

Have you tried with the following RewriteRule:

RewriteRule ^admin/?$ admin.php [L] 

Your .htaccess would look likes:

# mod-rewrite engine RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^admin/?$ admin.php [L] RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L] 

Update 1

and if you try the RewriteRules in the following order:

RewriteRule ^.*$ - [NC,L] RewriteRule ^.*(admin).*$ admin.php [NC,L] RewriteRule ^.*$ index.php [NC,L] 
1

This should do the trick:

RewriteEngine On # handle requests to /admin/ RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteCond %{REQUEST_URI} ^/admin/$ RewriteRule ^.*$ admin.php [NC,L] # handle all other requests RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L] 
1

To un-complicate things, you could have used a common boostrap script which would conditionally include the files. For example:

RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ bootstrap.php [NC,L] 

Then, in bootstrap.php

<?php if(USER_IS_ADMIN){ require('admin.php'); }else{ require('index.php'); } 

If you really need to check via htaccess, you might like to see this SOF thread

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like