penguinrob
penguinrob

Reputation: 1459

How to GET to a clean url using php

If I have a GET search field, how could I have that submit to a clean url (e.g. /search/keyword instead of index.php?fn=search&term=keyword)? I don't want to use Javascript to overwrite the submit event of the form if at all possible.

Thanks.

Upvotes: 0

Views: 2415

Answers (4)

AndiPower
AndiPower

Reputation: 853

You make a POST call to your index.php and then redirect to a nice URL.

index.php:

<?php
if (!empty($_POST["fn"])){
  $search = $_POST["fn"];
  $keyword = $_POST["term"];

  header("Location: http://www.yourwebsite.com/$search/$keyword");


}else{

 // Show your content

}

?>

Of course you have to use mod_rewrite to process you nice URL.

Upvotes: 0

nyxthulhu
nyxthulhu

Reputation: 9752

I assume from your tags (.htaccess) you are using apache, if so you could make use of the mod_rewrite engine to make your query strings SEO friendly.

Theres some very nice resources around the web like this one

Essentially what you need to do is

Ensure mod_rewrite is installed/enabled on the server

Switch on mod rewrite and configure rules

(as per a nice resource here), to convert from

http://www.best-food-of-the-usa.com/index.php?operation=top&state=arizona& city=mesa&limit=10

to

http://www.best-food-of-the-usa.com/arizona/mesa/top10.html

you can use the rule

RewriteRule ([a-zA-z]+)/([a-zA-z]+)/top10\.html$ index.php?operation=top&state=$1&city=$2&limit=10

Obviously you can make this rule either simpler and more generic to handle all your query strings, or you can make it customised for every url you want to rewrite.

Remember and update your app so it points to the new re-write urls though!

Upvotes: 2

Evert
Evert

Reputation: 99541

You must use javascript. You could redirect the PHP script to go to the 'clean' url, but even then you will first still go to the 'ugly' url.

Upvotes: 0

Herr
Herr

Reputation: 2735

I strongly recommend that you use a Framework like CodeIgniter or CakePHP. Both of them have the clean url ability and much more.

Upvotes: -1

Related Questions