Simple mod_rewrite | index.php?keyword=$1

Discussion in 'PHP' started by brianj, May 11, 2009.

  1. #1
    Hi, i have again a simple problem i can't get solved:

    
    RewriteEngine on
    RewriteBase /
    RewriteRule ^(.*)$ index2.php?keyword=$1 [L]
    PHP:
    //those url's i want to be recognized
    h*tp://www.domain.com/keyword
    h*tp://www.domain.com/keyword/
    h*tp://www.domain.com/keyword/%&$"$§-random-crap
    h*tp://www.domain.com/keyword%&$"$§-random-crap


    Anyone can help me getting all 4 to work without 404?


    Thanks alot,

    Brian
     
    brianj, May 11, 2009 IP
  2. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I would change ^(.*)$ into

    ^keyword /index2.php [L,QSE]
    ^keyword/(.*)$ /index2.php?keyword=$1 [L]
    ^keyword(.*)$ /index2.php?keyword$1 [L]

    or something like that. you also didn't have a path in front of your index2.php
     
    kblessinggr, May 11, 2009 IP
  3. brianj

    brianj Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Hey much thanks, but it's not fully working..

    //This is what i have in my index2.php
    $_GET['keyword']; 
    PHP:
    //this should point to index2.php
    h*tp://www.domain.com/keyword
    h*tp://www.domain.com/keyword/
    h*tp://www.domain.com/keyword/%&$"$§-random-crap
    h*tp://www.domain.com/keyword%&$"$§-random-crap



    How can i also detect characters in keyword that are not letters or "_" ?
    ??
     
    brianj, May 11, 2009 IP
  4. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #4
    What exactly are you looking for? Maybe my blog post will help if you want to check that out, but I'll try to write something. What is the error? Try commenting out your "RewriteBase /"

    Regards,
    Dennis M.
     
    Dennis M., May 11, 2009 IP
  5. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Maybe the best route is not to try to do all of that in the .htaccess but simply send everything after /keyword/ to index2.php and have PHP parse out whatever you try to put behind /keyword/, kinda like how wordpress parses out whatever comes after /

    A really quick and dirty way to parse (assuming the rewrite sent everything as q=) would be to do something like
    $queries = explode("&", $_GET['q']);
    $anitem = explode("=", $queries[2]); (though a foreach loop would be more handy to assigning these to a new associative array).
     
    kblessinggr, May 11, 2009 IP
  6. brianj

    brianj Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    thanks, seems complicated..

    Basically i just want domain.com/keyword to allways be printed out on my index2.php(or index.php) with
     $_GET['keyword'];
    PHP:

    but also domain.com/keyword"§$%&/ etc. not causing error messages..

    Any ideas`?
     
    brianj, May 12, 2009 IP