RSS Feed

Posts Tagged ‘mod_rewrite’

  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.


  2. Personalizing a page

    February 10, 2010 by Austin Hallock

    Personalizing pages I’ve found to be very powerful. “Hey Austin!” is more effective than “Hey You!”, I think it’s just because our name naturally stands out to us.

    mod_rewrite

    This can be really useful in emails you mail out to your list … you can have them click a link that brings up a page customized with their name.

    Here’s an example of what the code below will do for you (with my name in there): http://www.austinhallock.com/splash/austin

    Problem is (well, I guess this is a good thing for you) no one really does it even though it’s dead simple.

    First, name the page mypage.php rather than mypage.html.

    Next, when you send out an email linking to your page (assuming you’re using some auto-responder), link to http://www.yoursite.com/mypage.php?name={!firstname} — where I have {!firstname} you would put whatever code your auto-responder uses for the name.

    In your page where you want the name to show up put

    <?php if(isset($_GET['name']) && strlen($_GET['name']) > 0) $name = ucfirst($_GET['name']); else $name = "You"; echo $name; ?>
    

    This will grab their name if it’s in the URL, capitalize the first letter of it (ie. austin turns to Austin) and output it. If their name isn’t in the URL, it will output “You” instead. If you want it to say something other than “You” just change what’s in the quotes there.

    If you want it to show something other than “name” in the URL, for example you want it to look like: mypage.php?awesomeperson=austin just change all references of “name” in the above code to “awesomeperson”.

    To get a bit advanced and have the URL look prettier like http://www.yoursite.com/mypage/austin you’ll want to edit the .htaccess file in the folder your file is in. If you already have a .htaccess file in that folder just open it up and edit it in whichever editor you use. Otherwise, create one — it’s just named .htaccess nothing else tacked on.

    At the top of your .htaccess file you’ll need to put

    RewriteEngine on
    RewriteRule ^mypage(/|$)([^/\.]*)/?$ mypage.php?name=$2 [L]
    

    Of course replacing “mypage” and “mypage.php” with the folder you want it to appear like it’s in, and the file name.

    For example if you want yoursite.com/splash1.php?awesome=austin to show up as yoursite.com/splash/austin, you would change the first “mypage” to “splash”, “mypage.php” to “splash1.php” and “name” to “awesome”. You’d end up with something like this:

    RewriteEngine on
    RewriteRule ^splash(/|$)([^/\.]*)/?$ splash1.php?awesome=$2 [L]
    

    Hope this helps, and as always if you have any questions post them in the comments!