treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Rewrite /a/username/p/posts ?

  • I've got username working just fine, but I have a "show more posts" button which goes to profile.php?name=$username&l=$limit + 10 So I need to rewrite that one as well.
    Here is my rewrite for the /a/Username thingy:
    RewriteRule ^a/([^/]+)$ profile.php?name=$1
    Can anyone do anything like this where it's more like ?name=$1&l=$2 and then go /a/$1/p/$2 ?
  • Are you using a CMS for this? or going commando and writing your own?
  • No just rewriting if that's what you meant? I just put the script I wrote in my .htaccess file and then /a/username work's just like /profile.php?name=username

    I want profile.php?name=username&l=10 to corespond to a/username/10 ( I know I wrote /p/10 above, but it doesn't really matter
  • Thank you, but that didn't cover multiple variables. I've Google'd a bit, but I can't seem to make it work with it :/

    I want
    profile.php?name=Schart&l=20
    To go to
    /a/Schart/20
  • hmmm I don't know enough about mod_rewrite
    I normally use one variable then break it into an array using
    $url = explode('/', rtrim($_GET['url'],'/'));
    that way /username/10 would equal to $url[0]='username'; $url[1]='10';

    It also means you can have really long URLs

    *nb the rtrim is to stop an additional slash from breaking it
  • Firstly I don't think you're example is particularly safe, as ([^/]) matches exeverything except '/'. There are other characters you'll want to ignore as well as they are non URL safe, so it's best to write the rule with the characters you want

    RewriteRule ^a/([a-zA-Z0-9\-_]+)/([0-9]+)$ profile.php?name=$1&l=$2


    This will match anything like a/Schart-Test_URL01/20

    if you want the preceeding '/' before the 'a' then add one /a/([a-zA-Z0-9\-_]+)/([0-9]+)$

    Try your new rules on the following tool http://gskinner.com/RegExr/