Reputation: 10251
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!
Upvotes: 1
Views: 5939
Reputation: 10864
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]
.
Upvotes: 1
Reputation: 321
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
Upvotes: 1
Reputation: 3756
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]
Upvotes: 1
Reputation: 655129
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]
Upvotes: 2