views:

215

answers:

4

Hi guys,

I am using .htaccess and mod_rewrite in my little PHP framework. It's working nicely, but I want to expand on just having it redirect everything to index.php.

My directory structure is something like this (obviously simplified)

apps
media
system
-- admin
jscript
templates

My current rewrite rules is:

RewriteEngine On
RewriteCond $1 !^(index\.php|media|jscript)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

As you can see, everything 'mydomain.com/url' is redirected to index.php. What I would like to do now is allow anything 'mydomain/admin/' to direct to the 'admin' directory inside the 'system' folder. I want normal conditions inside this folder too, so rather than use clean users, I would use urls like 'mydomain.com/admin/some-kind-of-file.php'.

In a nutshell, I just want my rewrite condition to apply to everything except my system/admin folder.

Is this possible? Any help would be greatly appreciated!

+2  A: 

Just add admin to your condition and use an additional rule to redirect it:

RewriteCond $1 !^(index\.php|media|jscript|admin)/
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^admin/.* system/$0 [L]
Gumbo
Adding the trailing slash to the RewriteCond is a good idea, except that it will try to match "index.php/"
Nick
+1  A: 

It seems your 'media' and 'jscript' folders are already exempted from the rewrite rule, couldn't you just add the 'admin' folder too? eg:

RewriteEngine On
RewriteCond $1 !^(index\.php|media|jscript|admin)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

If you want to actually redirect everything under /admin to /system/admin (which will update the URL in the user's browser too) you'd use a mod_alias rule like this:

RedirectMatch permanent ^/admin/(.*)$ http://mysite.com/system/admin/$1
Nick
Thanks for the response Nick. I'm sorry if my post wasn't clear. The admin folder is a subdirectory of the 'system' folder, meaning the above won't work. Whilst I could have system/admin as a rewrite condition, I want to actually redirect anything mydomain.com/admin to mydomain.com/system/admin
Hanpan
I misunderstood the question... have edited post to do what I think you're after
Nick
+1  A: 

Redirect the people heading to the admin folder first, then filter the rest, such as:

RewriteRule ^admin/([^/]*)$ system/$1 [L,QSA]

RewriteCond $1 !^(index\.php|media|jscript)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Meep3D
I tried this originally, funnily enough. For some reason it doesn't work. If I type in the following:mydomain.com/admin/It redirects me here:mydomain.com/system/admin/?I have no idea where the ? is coming from!
Hanpan
Try this:http://www.addedbytes.com/apache/mod_rewrite-cheat-sheet/I've had a copy glued to my wall for the last year or so.
Meep3D
+1  A: 

It appears that the following only applies the RewriteRule if the URL doesn't start with site.com/index.php, site.com/media or site.com/jscript.


RewriteEngine On
RewriteCond $1 !^(index\.php|media|jscript)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

I'll be honest, I hadn't seen $1 used in a RewriteCond until now, but seems interesting.

So what if your url is site.com/admin...? Well you need a new RewriteRule. Also you need to prevent URLs beginning with admin get redirected by the first RewriteRule.


RewriteEngine On
RewriteCond $1 !^(index\.php|media|jscript|admin|system)
RewriteRule ^/?(.*)$ index.php/$1 [L,R]

RewriteRule ^/?(admin.*)$ system/$1 [L,R]

Update 1: added system to the list of URLs that shouldn't be applied to the first RewriteRule.

Update 2: removing [L,QSA] from first RewriteRule and replacing with [L,R].

PP
Odd...mydomain.com/admin/test.phpredirects to:mydomain.com/system/admin/?install.phpAgain, no idea where the ? is coming from! Perhaps there is something wrong with my apache install. I can't see why this wouldn't work!
Hanpan
^/? <-- why is the ? before (admin.*) - could that be part of it?
Meep3D
I could be wrong about the leading slash. I'm not sure how Apache deals with it because it's part of the URL the browser requests.. I just wanted to make a leading slash optional (the `?` is a regexp operator to mean zero-or-one).
PP
Actually why is the `[QSA]` there? I would try with `[L,R]` instead - the `[R]` means inform the browser to redirect to the new URL.
PP
Ooooh and you need to stop the /system URLs getting redirected too..
PP