URL Rewriting for search.

Discussion in 'PHP' started by baris22, Oct 27, 2008.

  1. #1
    hello there.

    I am trying to use rewrite mod for the url (htaccess)
    Everything is fine but i do not know what to do on search page. I am just using a form for the search like:

    
    
    <form name="form" id="form" action="http://www.site/search.php" method="get" enctype="multipart/form-data">
    												  <table style="height:20px;">
    														<tr>
    															<td width="218" style="height:20px;"><input class="input_1" type="text" name="q"></td>
    															<td><input type="submit" name="Submit" value="Search" STYLE="width:6em; height:2em"></td>
    													</tr>
    													</table></form>
    
    PHP:
    I managed to get the htaccess for it which is:

    
    
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule search-q-(.*)-Submit-(.*)\.htm$ search.php?q=$1&Submit=$2	
    
    
    PHP:
    but i do not know how to change the search form according to this htaccess.

    Any idea?

    Thanks
     
    baris22, Oct 27, 2008 IP
  2. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Your htaccess and forms is correct, like if someone goes manually to http://www.site/search-q-test-Submit-Search.htm it will open your search page. but you need to automatically transfer your visitors to this url.

    For this, use below .htaccess code:
    
    RewriteCond %{QUERY_STRING} q=(.*)&Submit=(.*)
    RewriteRule search.php /search-q-%1-Submit-%2.htm? [R=301,L]
    RewriteRule search-q-(.*)-Submit-(.*).htm$ search.php?q=$1&Submit=$2 [L]
    
    Code (markup):
    It will automatically transfer your visitor from your Form page like http://www.site/search.php?q=test&Submit=Search to http://www.site/search-q-test-Submit-Search.htm

    Thanks, hope it helps!
     
    mehdi, Oct 27, 2008 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3

    what about if i want to do this


    
    
    http://www.site/search.php?q=test
    
    
    PHP:
    instead of this

    
    
    http://www.site/search.php?q=test&Submit=Search 
    
    
    PHP:
    and if i change the htaccess code to

    
    
    RewriteRule search/q/(.*)/ search.php?q=$1
    RewriteRule search/q/(.*) search.php?q=$1
    
    
    PHP:
    What kind of htaccess i would need?

    Thank you very much.
     
    baris22, Oct 28, 2008 IP
  4. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #4
    ok..

    I managed to get it working.

    I used:

    
    
    RewriteEngine on
    RewriteCond %{QUERY_STRING} q=(.*)
    RewriteRule search/%1? [R=301,L]
    RewriteRule search/(.*)$ search.php?q=$1 [L]
    
    
    PHP:
    the output is:

    http://www.site.com/search/xxxxx

    but i have got a problem. if there is second page for the search i do not know what to do.

    this is the php code for second page:

    
    
    <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 &gt;&gt;</a
    
    
    PHP:
    and this is the one for previous page:

    
    
    <a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt; 
      Prev 10</a>
    
    
    PHP:
    Thanks
     
    baris22, Oct 28, 2008 IP
  5. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #5
    The way I do this is to make an intermediate page called something like searchredirect.php:

    
    $SearchURL = $SiteRoot.'/search/'.$_GET ['q'];
    header("Location: $SearchURL");   /* Redirect browser to the new address */
    
    Code (markup):
    Then you rewrite '/search/sausages' to '/search.php?q=sausages' in .htaccess. That makes it transparent to the user and creates pretty much the same effect. I just found it simpler. Maybe someone can see a disadvantage to this method..?
     
    markowe, Oct 28, 2008 IP
  6. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    This might not be good for google.
     
    baris22, Oct 28, 2008 IP
  7. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #7
    Maybe... what do other people think? I think redirecting pages within my own site for the purposes of rewriting them is my own business, not Google's... It's another matter if I was trying to cloak something.

    Anyway, I guess Google DOES index search results pages, and they can be a good source of "dynamic content", but they are not a priority anyway... As long as Google is not complaining about my "main" content pages.
     
    markowe, Oct 28, 2008 IP
  8. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #8
    For the normal search form i am not bodered about my urls. but i am using this search function as a tag clouds.

    My purpose in here is to get a nice url. I am linking certain keywords on the page.
     
    baris22, Oct 28, 2008 IP