0

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.

Josh
  • 8,082
  • 5
  • 43
  • 41
penguinrob
  • 1,431
  • 3
  • 17
  • 39

4 Answers4

2

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!

John Mitchell
  • 9,653
  • 9
  • 57
  • 91
  • There are many ways out there to make it work without having to use it this way. Any modifications like rewriting URLs as mentioned above using mod_rewrite will only degrade already sucking Apache's performance. – Anvesh Checka Feb 28 '14 at 10:23
0

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.

AndiPower
  • 853
  • 10
  • 20
0

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.

Evert
  • 93,428
  • 18
  • 118
  • 189
  • I ended up just using javascript and coding a separate ugly url that the non-javascript form could submit to. Much easier than I thought it would be. – penguinrob Nov 25 '11 at 23:25
-1

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

Herr
  • 2,725
  • 3
  • 30
  • 36
  • That would be fine if I had started with one of those frameworks, but alas, I'm already a good portion into this project and ran into this issue. – penguinrob Nov 25 '11 at 23:00
  • well you are doomed from the beginning. Good luck – Herr Nov 26 '11 at 10:56