1

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 http://www.mydomain.com/admin/

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:

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

2
  • You could copy this .htaccess file and put it in the /admin folder with the new rule changing index.php to admin.php Commented May 9, 2014 at 20:56
  • Sorry for me not being clear enough. The index.php and admin.php file are both in the root directory. The application need to switch between the two if /admin/ or not. Hope this makes it more understandable. Commented May 9, 2014 at 21:03

3 Answers 3

1

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] 
Sign up to request clarification or add additional context in comments.

1 Comment

Such a simple addition to my current code, but solved the problem! First entry didn't work, but with the update it works like a charm :D
0

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 Comment

I've tried your solution but for some reason it skips you addition and just autoload the index.php. I've tested it by turning index.php and admin.php around with a helloworld echo. It's shows me the helloworld on every page /some/things and loads the index.php only without any additional routes.
0

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 Comments

Simple and real straightforward solution, but would like to handle it within my .htaccess. I'm not totally sure on which part you're referring in the SOF thread. Can you be a bit more specific?
My bad, it looks like that answer wouldn't help too much in this case. Just had a through read :)
I was trying to make something out of the thread but could figure out what you ment haha. No problem, maybe also my explanation skills arent the best in the world :p
@Wimbjorno I did some search on this. Interesting approach I must say. How about using cookie ? You could add a rewrite condition like for example : RewriteCond %{HTTP_COOKIE} ^.*user_id=(\d+).*$ [NC]
I've cleared up my question a bit more. I appreciate you help but I'm not sure if you understand my question. Could you please read everthing after EDIT. Hope it make a bit more sense :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.