SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can Mod_Rewrite do this?

    Ive been trying to make this happen and I think mod_rewrite might be able to do this, but I'm not sure how to dynamically change all .php requests. Nor do I know if this is a good idea.

    Basically it would be really tedius to make rules for every single page, every time I add a controller.

    Open
    <pagename>.php

    But it really loads
    system/application/controllers/<pagename>.php

    Yet stays on the <pagename>.php url

    I don't know if this works
    %{REQUEST_FILENAME}

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, that's exactly what mod_rewrite does.

    But might I suggest adding a front controller so that you can add more logic in the future if necessary? Say you want to handle pagename.rss in the future to call a different controller that generates RSS feeds.

    Rewrite all requests that aren't to actual files to index.php (or any name), and index.php looks at the REQUEST_URI and includes system/applicaton/controllers/pagename.php

    Rewrite rule:
    Code:
    RewriteCond &#37;{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    basic index.php:
    PHP Code:
    <?php
    $pagename 
    substr($_SERVER['REQUEST_URI'], 1strlen($_SERVER['REQUEST_URI']) -
     
    5);
    include(
    "system/application/controllers/" $pagename ".php");
    The URL in the address bar remains pagename.php

  3. #3
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow that is very clever, I like that with the index php file.
    Thanks for the fast reply I wasnt prepared for that on this topic

    Awesome Ill play around with this for a while, thanks!

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,671
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    JR,

    Dan is correct, that's exactly what mod_rewrite does.

    However, what he showed you is the WP code to force the index.php script to handle EVERYTHING. That is NOT mod_rewrite but an effective use of PHP in the "404'd" request (WP's code, which Dan showed you, is quite effective as a 404 handler without using Apache's inbuilt ErrorDocument 404 {path/file}.

    Unfortunately, it's NOT what you were asking for: A simple mod_rewrite to perform redirections.

    First, you should NOT be using mod_rewrite for that (as a general rule) because mod_alias's Redirect is designed to do that even faster.

    Second, I use something similar to what you are asking for (in the DocumentRoot, though) at my website (for extensionless URIs) with:
    Code:
    RewriteEngine on
    RewriteRule ^([a-z]+)$ $1.php [L]
    All you would need to do is stick your system/application/controllers/ in front of the $1 in the redirection to have the same thing. Just be sure that the script knows it's three levels deeper into your website as relative links will be predicated on the DocumentRoot level of the {REQUEST_URI}.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Thanks, Ill try that too, Ive been pickling with it a bit.Im getting this error: Forbidden
    You don't have permission to access /fk2/system/application/controllers/index.php on this server.

    My .htaccess
    RewriteEngine On
    RewriteRule ^([a-z]+)$ system/application/controllers/$1.php [L]


    I don't have a .htaccess inside the controllers area, and there is a index.php file.
    But I don't think that should matter, ill try it anyways though

    ### EDIT ###
    When I do this it allows it to work, is this how i should keep it?

    .htaccess in /controllers/
    Order Allow,Deny
    Allow from All


    I don't want to have to edit apache.conf (i think thats the file) since this will be distributable and it's gotta stay simple :P

    For some reason I have a hard time with the apache stuff, I still haven't really GOT IT yet haha, even after playing with it quite a bit in the past.

  6. #6
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Question:
    How come this doesn't change when submit is clicked?
    <form action="login.php" method="post">
    "The requested URL /fk2/login.php was not found on this server."


    I am only guessing i need a &#37;REQUEST type of thing in the htaccess, I tried this without any luck

    Code:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([a-z]+)$ system/application/controllers/$1.php [L]

  7. #7
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay this works better for me

    RewriteRule ^([a-z.-]+)$ system/application/controllers/$1 [L]

    Then I can do -- page.php?query=val&string=X
    which is how i prefer :P

  8. #8
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,671
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    JR,

    If there is a directory protection in any of the directories in the path, you SHOULD receive that message!

    As for your order code, that's associated with a <Directory> or <File> block, it should NOT be standalone!

    Your login.php relative link will be to the same directory level as indicated by the browser's location, i.e., fk2. That's where your .htaccess is located, isn't it? Your "how come" is that login (NOT login.php) would have been redirected to system/application/controllers. When you go play with directory levels, you've GOT to remember what you're doing with these funky links! The same thing goes with your page.php in the third post.

    Finally, yes, the -d and -f tests are advisable - if you can have your "application" serving files you're not in control of (in other people's file systems). I.e., not advised for your case.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  9. #9
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THanks a lot!
    Haha Im having a good old time tweaking all this stuff I almost got it, now its up to some PHP stuff only :P


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •