Treehouse: Grow your CSS skills. Land your dream job.

Rewrite /a/username/p/posts ?

  • # February 26, 2012 at 12:36 pm

    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 ?

    # February 26, 2012 at 12:55 pm

    Are you using a CMS for this? or going commando and writing your own?

    # February 26, 2012 at 1:04 pm

    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

    # February 26, 2012 at 2:30 pm

    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
    To go to

    # February 26, 2012 at 6:32 pm

    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,'/'));

    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

    # February 27, 2012 at 3:50 am

    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

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.