Apache mod_rewrite and using RewriteRule for directories

  • samir22
  • Born
  • Born
  • samir22
  • Posts: 2

Post 3+ Months Ago

Hi,
I have a question.
I'm using Yii framework, and i'm trying to figure out how to set up two apps in one single project.
So far so good, on the code side, i think I got it working.
However, my lack of strong knowledge on mod_rewrite takes its toll, and i'd like to know if anyone can help me, it shouldn't be very difficult for Apache gurus out there.

Here's my .htaccess so far:

Code: [ Select ]
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php
  1. Options +FollowSymLinks
  2. IndexIgnore */*
  3. RewriteEngine on
  4. # if a directory or a file exists, use it directly
  5. RewriteCond %{REQUEST_FILENAME} !-f
  6. RewriteCond %{REQUEST_FILENAME} !-d
  7. # otherwise forward it to index.php
  8. RewriteRule . index.php


Now, this routes all requests to the main index.php file which determins what to do next.
What i'd like to know, is what rule I have to set up so that when /admin is located in the url,
to redirect to index-admin.php instead of index.php

So basically, if the url is like: /admin or /admin/something.*, to point to index-admin.php, else to point to index.php, like now.

if anyone can help i'd be grateful.
thanks
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9102
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Above your:

Code: [ Select ]
RewriteRule . index.php


Just put some rules in that you would like. That rule you have there with the single period is basically like a catchall which says rewrite any URL to index.php. However for ones above that you would use something more specific and use something like:

Code: [ Select ]
RewriteRule ^admin/.* index-admin.php [L,NC]


That would say that anything in the admin directory rewrite to index-admin.php. In the last part there where it says [L,NC] the L basically means this is the last rewrite, do not proceed any further, stop there; and the NC means to ignore case. So in the end you would have something like this:

Code: [ Select ]
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# additional rules
RewriteRule ^admin/.* index-admin.php [L,NC]

# otherwise if nothing matches above forward it to index.php
RewriteRule . index.php
  1. Options +FollowSymLinks
  2. IndexIgnore */*
  3. RewriteEngine on
  4. # if a directory or a file exists, use it directly
  5. RewriteCond %{REQUEST_FILENAME} !-f
  6. RewriteCond %{REQUEST_FILENAME} !-d
  7. # additional rules
  8. RewriteRule ^admin/.* index-admin.php [L,NC]
  9. # otherwise if nothing matches above forward it to index.php
  10. RewriteRule . index.php
  • samir22
  • Born
  • Born
  • samir22
  • Posts: 2

Post 3+ Months Ago

Hi.
Thanks for replying.
The problem is I tried your code and it won't work.
Here's my directory structure

test
.htaccess
index.php
index-admin.php

I have a virtual host for: test.localhost, with allow override all

I tried the code, when I type in test.localhost/admin, still the index.php is returned
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9102
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

The way I wrote the code above, your link would have to have the final backslash on it and anything under it would work. If you typed it just like:

test.localhost/admin

that would not match. If you typed any of these:

test.localhost/admin/
test.localhost/admin/somepage.php

That should all work. If you want it to work without the final /, you will need to remove that from the RewriteRule.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 37 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.