Rewriting url after search form

Discussion in 'Programming' started by colin008, Jan 4, 2007.

  1. #1
    I have seen this on many sites where after searching via a search form, the search result will be part of the friendly url

    ie.
    <form action="/find.php" method="get"> the url would be
    /find.php?search=dog

    However the friendly url would be
    /find/dog.html

    may be this can be achived through JavaScript or PHP with mod _rewrite i dont know...

    Any help would be great ?
     
    colin008, Jan 4, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    RewriteEngine On
    
    RewriteRule ^find\/(.+)\.html$ find.php?search=$1
    
    Code (markup):
    Untested.
     
    nico_swd, Jan 4, 2007 IP
  3. colin008

    colin008 Active Member

    Messages:
    162
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    68
    #3
    Yes i am using that Rewrite all ready, thanks and if i had a link in my page /find/dog.html that will work great.

    However if the search form is on the page /find/dog.html
    after clicking the form button searching for "cat" the url will be /find/dog.html?search=cat

    There must be a way to change the url to /find/cat.html
    But i dont know what it is..!
     
    colin008, Jan 4, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    You could do.

    
    <form action="" method="get" onsubmit="window.location='/find/'+ this.fieldname.value +'.html'; return false;">
    
    
    Code (markup):
    (Replacing "fieldname" with the actual field name.)


    Note that this won't work if Javascript is disabled on the client's side.
     
    nico_swd, Jan 4, 2007 IP
  5. colin008

    colin008 Active Member

    Messages:
    162
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    68
    #5
    Spot on nico_swd and thank very much, it makes me smile.
     
    colin008, Jan 5, 2007 IP