如何获得一个干净的url使用php


How to GET to a clean url using php

如果我有一个GET搜索字段,我怎么能把它提交到一个干净的url(例如/search/keyword而不是index.php?fn=search&term=keyword)?如果可能的话,我不想使用Javascript覆盖表单的提交事件。

谢谢。

我假设从你的标签(.htaccess)你正在使用apache,如果是这样,你可以利用mod_rewrite引擎,使您的查询字符串SEO友好。

网上有一些非常好的资源,比如这个

实际上你需要做的是

确保在服务器上安装/启用了mod_rewrite

打开mod重写并配置规则

(根据这里的一个很好的资源),从

转换

http://www.best-food-of-the-usa.com/index.php?operation=top&状态= arizona&城市= mesa&限制= 10

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

你可以使用

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

显然,你可以让这个规则更简单和更通用来处理你所有的查询字符串,或者你可以为你想要重写的每个url定制它。

记得更新你的应用,让它指向新的重写的url !

必须使用javascript。你可以将PHP脚本重定向到"干净"的url,但即使这样,你还是会先到"丑陋"的url。

对index.php进行POST调用,然后重定向到一个漂亮的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
}
?>

当然你必须使用mod_rewrite来处理你漂亮的URL

我强烈建议您使用CodeIgniter或CakePHP这样的框架。他们都有干净的url能力和更多。