从 _GET 查询创建 SEO 友好的 URL

Create SEO Friendly URLs from _GET query

我有一个使用 GET 请求的搜索表单。当我按下回车键时,url 像往常一样对 SEO 不友好,有没有办法让它在输入时显示 SEO 友好 urls?

例如

GET Request http://someurl.com?a=search&query=what+are+you+looking+for
SEO URL should be http://someurl.com/search/what+are+you+looking+for

我已经创建了规则

RewriteRule ^search/(\w+) index.php?a=search&query=

在我的 .htaccess 中,当我在地址栏中手动输入 SEO Url 时,它会起作用。所以我想这只是确保当我在搜索字段中按下回车键时,它会加载 SEO Url 而不是另一个。

您可以在 .htaccess:

中使用它
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?a=&query= [L]

这会给你留下 URL:

http://someurl.com/search/what+are+you+looking+for

您必须将原来的 uri 重定向到新的 uri,在现有规则之前添加以下内容:

RewriteEngine on
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?a=search&query=(.+)\sHTTP [NC]
RewriteRule ^ /search/%1? [NE,L,R]

我在路上这样做:)

这是用于搜索的表格

  <form method="post">
     <input type="text" name="search" placeholder="search"/>
  </form>

表格前

if(isset($_POST['search'])){
echo '<script>window.location = "www.domain.com/search/'.$_POST['search'].'"</script>';
}

并在.htaccess

RewriteEngine on
RewriteRule ^search/([^/]*)$ index.php?page=search&search= [NC,L]