RSS Feed

‘mod_rewrite’ Category

  1. Fun with mod_rewrite

    March 25, 2010 by Austin Hallock

    Ever wanted your page that has an ugly URL like:
    /index.php?page=whatever&long=2&parameter=3
    to look like /whatever/2/3?

    It’s actually pretty easy and done through something called mod_rewrite.

    Mod_rewrite can get pretty complicated and get into a ton of regular expressions, but for most uses, just copy and paste what I have and make any necessary changes.

    Mod_rewrite goes in the .htaccess file on your server (in the main public html directory). If your server has it, edit the file, if not create one — it’s just .htaccess, nothing before the period.

    Here’s the base code you’ll want to start with (but will need some modification) All of this is color coded so hopefully that helps:

    RewriteEngine on
    RewriteRule ^([^/]+)/?$ index.php?page=$1&%{QUERY_STRING} [L]
    

    For example, if you want /index.php?action=anything to look like /anything what you’ll need is:

    RewriteEngine on
    RewriteRule ^([^/]+)/?$ index.php?action=$1&%{QUERY_STRING} [L]
    

    The above code will make /login essentially cloak /index.php?action=login, /register will load/cloak /index.php?action=register etc…

    If you only want /login to go to /index.php?action=login and not have it happen for /anything you would want something like this (no wildcard):

    RewriteEngine on
    RewriteRule ^login/?$ index.php?action=login&%{QUERY_STRING} [L]
    

    You can also do this for multiple parameters, so to go back to that first example of making /index.php?page=whatever&num1=7&num2=6 look like /whatever/7/6 you would want to use:

    RewriteEngine on
    RewriteRule ^([^/]+)/([0-9]+)/([0-9]+)/?$ index.php?page=$1&num1=$2&num2=$3&%{QUERY_STRING} [L]
    
    • Here’s a bit of an explanation of what that all means…Starting at the 2nd line, the rule is it’s searching the URL (when someone visits a page on your site) for ([^/]+) which is any combination of characters/numbers *except* a forward slash. The + means it must match at least one character.
    • ([0-9]+) Looks for any number
    • /?$
      • The / looks for a forward slash
      • The ? means that forward slash is optional (if it’s there it works if it’s not it still works)
      • $ means it’s at the end of the URL. So it matches for http://anything.com/anything/ and http://anything.com/anything, but not http://anything.com/anything/somethingelse.
    • After the space is the URL it will load once there is a match
      • $1 is a wildcard for the first contents of parenthesis, so it refers back to (^/) and what it stood for. In this case it stood for “whatever
      • $2 is a wildcard for the 2nd set of parenthesis, $3 for the third and so on.
      • %{QUERY_STRING} just allows extra stuff to be put on the end so if I did /whatever/7/6/?test=test it would pass the test parameter as well.

    Want to get into some more advanced mod_rewrite? Here’s a nice little cheat sheet. If you have questions, post them in the comments.