search criteria

Discussion in 'PHP' started by webboy, Mar 27, 2008.

  1. #1
    Hi all just a quick question,

    How is this website do this as I have been looking a the URL EG hXXp://www.nextag.com/testbox

    And it passes anything after the domain name as the search criteria without using a “ ? “

    I can only imaging it could be a .htacccess file doing the process but how?
     
    webboy, Mar 27, 2008 IP
  2. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #2
    It'll probably be MOD Rewrite.
     
    Weirfire, Mar 27, 2008 IP
  3. webboy

    webboy Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    What would the coding look like i have seen something like you can pass $1 ect
    when you rewrite the url but how would that pass to the another url
     
    webboy, Mar 27, 2008 IP
  4. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #4
    Lets say the search term is "search" and the value is "widget".

    The search page might redirect to www.domain.com/search/widget and the MOD rewrite would change the URL to www.domain.com/index.php?search=widget but of course you wouldn't see this.

    You just need to point the search to a handler which will redirect the page such as ;

    <?php
    header('Location: http://www.domain.com/search/' . $_POST['search']);
    ?>
    PHP:
     
    Weirfire, Mar 27, 2008 IP
  5. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #5
    Exmaple of .htaccess
    
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.+?)$	/search.php?what=$1 [L,QSA]
    
    Code (markup):
    Where RewriteCond %{REQUEST_FILENAME} !-f will not rewrite queries to search.php and rewrite queries like
    http://site.com/elephant to http://site.com/search.php?what=elephant

    And in PHP you can access value like this
    
    $what_to_search = $_GET['what'];
    
    PHP:
     
    AsHinE, Mar 27, 2008 IP
  6. webboy

    webboy Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Oppps getting a 500 error what did it do wrong
     
    webboy, Mar 27, 2008 IP
  7. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #7
    I'm a bit of a MOD rewrite noob myself but try;

    RewriteRule ^(.+?)$ /search.php?what=$1 [L,QSA]
     
    Weirfire, Mar 27, 2008 IP
  8. webboy

    webboy Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    still getting a 500 error

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.+?)$ /search.php?what=$1 [L,QSA]

    the file is a folder called /jump
     
    webboy, Mar 27, 2008 IP
  9. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #9
    Where's Nintendo when you need him?
     
    Weirfire, Mar 27, 2008 IP
  10. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #10
    I've just tested it - it works on my hosting. Maybe you don't have mod_rewrite or it is configured in another way.

    For example http://mysite.com/lalala

    
    <?
    print_r($_GET);
    ?>
    
    PHP:
    outputs
    
    Array ( [what] => lalala )
    
    Code (markup):
     
    AsHinE, Mar 28, 2008 IP
  11. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #11

    To find out if your server has mod rewrite installed create a file called "php.php" and insert the following into it;

    <?php
    phpinfo();
    ?>

    Search the page for "mod_rewrite" and it should be listed in the category "Loaded Modules".

    Mod rewrite comes with Apache but can sometimes not be turned on but it's not too difficult to sort this out if it is the case.
     
    Weirfire, Mar 28, 2008 IP
    AsHinE and birdsq like this.