Mod Rewrite Anyone?

Discussion in 'Web Design and Development' started by Fleetwood Mac, Aug 25, 2007.

  1. Fleetwood Mac macrumors 65816

    Fleetwood Mac

    Joined:
    Apr 27, 2006
    Location:
    Canada
    #1
    I'm attempting to use mod rewrite to make URLs like http://mysite.com/index.php?page=imapyschokiller available at http://mysite.com/imapyschokiller. Ideally, I'd also like page such as http://mysite.com/index.php?page=go/with/the/flow to be available at http://mysite.com/go/with/the/flow too. Is this possible?

    The best way to help me here would be to post the exact code I need to put in .htaccess. I'd love to learn, but I just can't see myself ever understanding this stuff. Too bad I can't do it in CSS. :D

    I know, hopeless designer's question of the day™™, but just maybe someone can help me. :eek:
     
  2. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #2
    do you want *all* pages with that pattern to be re-directed? if so, something like:
    Code:
    RewriteEngine on
    RewriteRule ([^/\.]+)/?$ index.php?page=$1 [L]
    
    i'm no rewrite expert, but i think that should do the trick. there's a very similar example on this page.

    if you want to do only certain pages, then you'll have to specify each one:
    Code:
    RewriteRule ^imapyschokiller$ index.php?page=imapyschokiller [L]
    RewriteRule ^go/with/the/flow$ index.php?page=go/with/the/flow [L]
    
     
  3. Fleetwood Mac thread starter macrumors 65816

    Fleetwood Mac

    Joined:
    Apr 27, 2006
    Location:
    Canada
    #3
    I am trying to redirect all pages.

    With one modification (adding a ^ after RewriteRule), the code provided worked for top level pages.
    Code:
    RewriteEngine on
    RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
    So; http://mysite.com/index.php?page=ducklings is now accessible at http://mysite.com/ducklings/. However, urls contained below the domains root (such as http://mysite.com/index.php?page=clownfish/nemo) don't seem to be affected by the rule.

    In a perfect world, the pages would also be accessible without the trailing slash, but that isn't a matter of huge importance at this point.
     
  4. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #4
    it's all trial and error for me at this point, this is more than i've done before. however, give each of these a try (one at a time):
    Code:
    RewriteRule ^([^\.]+)/?$ index.php?page=$1 [L]
    RewriteRule ^([^\.]+)$ index.php?page=$1 [L]
    
    edit: i just tested it out, and the first one is working for me, with and without the trailing slash. here's my index.php:
    Code:
    <?php
    echo $QUERY_STRING
    ?>
    
    and when i hit http://www.mysite.com/foo/bar/baz, i get:
    Code:
    page=foo/bar/baz
    and when i hit http://www.mysite.com/foo/bar/baz/, i get:
    Code:
    page=foo/bar/baz/
     
  5. elppa macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #5
    This is what wordpress does:

    Code:
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
    </IfModule>
    
    Then everything gets sent to index.php.

    You can then use $_SERVER['REQUEST_URI'] you can get a address divvy it up from there.

    If no page exists you can send a: header("HTTP/1.0 404 Not Found"); and redirect to a 404 page.

    Probably not the best/most efficient way to do it, but it works for me. MOD REWRITE looks so complicated from trying to read the manual so I kind of gave up.
     
  6. Fleetwood Mac thread starter macrumors 65816

    Fleetwood Mac

    Joined:
    Apr 27, 2006
    Location:
    Canada
    #6
    Its working. Here's my .htaccess:
    Code:
    RewriteEngine On
    RewriteBase /
    RewriteRule ^([^\.]+)/?$ index.php?page=$1 [L]
    Thank you so much, zimv20 and elppa.
     
  7. CoreWeb macrumors 6502

    Joined:
    Mar 2, 2007
    Location:
    Edge of reason
    #7
    Keep in mind, though, that you might not want to redirect all content. I don't redirect the /templates folder on my website. Everything else is redirected. This allows me to statically store the CSS, Javascript, etc. for better performance, as the PHP-based system which parses the URL combined with the exceptionally slow server can make page generation times be somewhat slow (best case scenario, ~.03 seconds, worst case, with everything being generated from scratch, no cache or anything, 100% of CPU already being used by other processes, ~25 seconds... OUCH!)

    Apache's file-system based path interpretation is much, much faster than PHP. It will most likely take Apache < .00001 seconds, where as PHP will take significantly longer.
     
  8. Knox Administrator

    Knox

    Staff Member

    Joined:
    Jul 1, 2002
    Location:
    UK
    #8
    I suspect that's why the regex is exempting URLs with a period in them, so that things like style.css, image.jpg, javascript.js etc are ignored. However, good point in general - do need to be careful with rewrite rules, sometimes they have unexpected consequences.
     
  9. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #9
    awesome. glad i could help.
     

Share This Page