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...
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)
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]
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]
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