RewriteRule: bootstrap to two different files

Discussion in 'Apache' started by reeksy, Mar 7, 2007.

  1. #1
    Hi.

    I’ve got a .htaccess file in my web root. Using the .htaccess file I force every request through a single file (frontend.php). Here’s my .htaccess file:

     
    RewriteEngine on
    RewriteRule .* frontend.php
    
    Code (markup):
    As mentioned above, using this .htaccess file every request is forced through frontend.php, so

     
    http://www.mydomain.com/afile.php
    http://www.mydomain.com/dirOne/dirTwo/
    http://www.mydomain.com/index.php
    
    Code (markup):
    Will all run exactly the same script (frontend.php).

    This works fine, however I want to write a rule to so every request made to an admin alias goes through a separate file (backend.php). So, for example the following URLs would all go through backend.php as they all contain the admin alias.

     
    http://www.mydomain.com/admin/
    http://www.mydomain.com/admin/dirOne/dirTwo/
    http://www.mydomain.com/admin/index.php
    
    Code (markup):
    My problem is I’m unsure how to write this within my .htaccess file. Does anyone have any ideas! I’m completely stuck!

    Jon
     
    reeksy, Mar 7, 2007 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    RewriteRule ^admin(.*) backend.php [L]
    RewriteCond %{SCRIPT_FILENAME} !backend.php$
    RewriteRule .* frontend.php [L]
    Code (markup):
    That may need a bit more work but it should be something like that.
    Line 1: match all requests that begin with 'admin' and send to backend.php
    Line 2: compare script name to backend - this condition is only met if we're not using backend.php
    Line 3: send everything else to frontend.php

    This is where the [L] (last) flag can become a little confusing. In theory, you might be able to miss out the middle line in the above - send all admin requests to backend.php, use the L flag to stop processing and on the next line if we're still running send everything else to the frontend.php

    However, the L flag applies only to that request. So if we were to use just:
    RewriteRule ^admin(.*) backend.php [L]
    RewriteRule .* frontend.php [L]
    Code (markup):
    A request for admin/pie.php will be intercepted and rewritten to backend.php. The flag means we stop here for this request. mod_rewrite will now make a new request for the backend.php script - obviously the first rewriterule no longer applies but the second one does, so we in turn get rewritten again to frontend.php.

    Obviously that's not what we want, so we introduce a condition to the second rewriterule. So we're now only doing that second rewrite if we're not requesting the backend.php file.

    Hopefully that makes some sense. ;)
     
    rodney88, Mar 7, 2007 IP
  3. reeksy

    reeksy Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply.

    I will use the code examples you have provided when I get home tonight to test them out.

    Jon.
     
    reeksy, Mar 8, 2007 IP
  4. reeksy

    reeksy Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks rodney88 - this worked very well.

    Solved
     
    reeksy, Mar 15, 2007 IP