Formatting the search URL

Discussion in 'PHP' started by Silvers, Mar 10, 2009.

  1. #1
    So I have a search box with GET , the browser then redirects to search.php?q=search_query , where search_query is the search you make .
    I've also made SEO Friendly url's like search/search_query.html but those work only if you type them .
    How can I make so the PHP code will auttomatically direct me to search/search_query.html ?
     
    Silvers, Mar 10, 2009 IP
  2. wayfarer07

    wayfarer07 Peon

    Messages:
    34
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    if(isset($_GET["q"])) {
         header("Location: http://yourwebsitename/search/".$_GET["q"].".html");
    }
    
    PHP:
    At the top of the page, before any HTML is outputted.
     
    wayfarer07, Mar 10, 2009 IP
  3. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #3
    Also, you want to header 302 redirect it to make it google friendly.
     
    exodus, Mar 10, 2009 IP
  4. Silvers

    Silvers Well-Known Member

    Messages:
    675
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    108
    #4
    hey, I inserted the code in search.php and says the next message :
    I think I must put the code in other php file ( search1.php ) and set the input form to search1.php and the redirects to search.php ( without that in the header ) .
     
    Silvers, Mar 11, 2009 IP
  5. wayfarer07

    wayfarer07 Peon

    Messages:
    34
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Actually, that would be a 301 (permanently moved) header, and you don't need to do it here, because Google doesn't submit forms. If it is intended to link to the search queries using the ?q= protocol, sending a 301 would be a good idea, however.
    I would have to see more about your system and code to know for sure. It depends on how your URLs are being rewritten I think. Have you solved this by now?
     
    wayfarer07, Mar 11, 2009 IP
  6. adstiger

    adstiger Peon

    Messages:
    409
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I suggest you to use Apache redirects instead of php redirects. That saves a lot of load on your server.
     
    adstiger, Mar 11, 2009 IP
  7. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #7
    If you are not creating static html files ( depending on search results ), easier will be to do this from .htaccess ( domain.com/search-term actually points to domain.com/search.php?key=search-term ).
     
    ActiveFrost, Mar 11, 2009 IP
  8. Silvers

    Silvers Well-Known Member

    Messages:
    675
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    108
    #8
    @wayfarer : didn't resolved it , but I don't want to create another php file and redirect from one to another ...
    @adstiger : that sounds a little hard since I have a shared hosting ...
    @ActiveFrost : my htaccess looks like this :
    RewriteEngine On
    RewriteRule ^search/([^/]*)\.html$ /search.php?q=$1 [L]
    
    Code (markup):
    It works if you type the url ...
    I remember there was another code to redirect from htaccess ( from search.php to search/query.html ...
     
    Silvers, Mar 11, 2009 IP