如果我有一个 GET 搜索字段,我怎么能将它提交到一个干净的 url(例如/search/keyword
,而不是index.php?fn=search&term=keyword
)?如果可能的话,我不想使用 Javascript 来覆盖表单的提交事件。
谢谢。
如果我有一个 GET 搜索字段,我怎么能将它提交到一个干净的 url(例如/search/keyword
,而不是index.php?fn=search&term=keyword
)?如果可能的话,我不想使用 Javascript 来覆盖表单的提交事件。
谢谢。
我从您的标签 (.htaccess) 中假设您使用的是 apache,如果是这样,您可以使用 mod_rewrite 引擎使您的查询字符串对 SEO 友好。
网上有一些非常好的资源,比如这个
基本上你需要做的是
确保在服务器上安装/启用 mod_rewrite
打开 mod rewrite 并配置规则
(根据这里的一个不错的资源),从
http://www.best-food-of-the-usa.com/index.php?operation=top&state=arizona& city=mesa&limit=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!
您对 index.php 进行 POST 调用,然后重定向到一个不错的 URL。
索引.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。
您必须使用 javascript。您可以将 PHP 脚本重定向到“干净”的 url,但即便如此,您仍然首先会转到“丑陋”的 url。
我强烈建议您使用像CodeIgniter或CakePHP这样的框架。它们都具有干净的 url 能力等等。