php - search.php 页面的 htaccess 规则

标签 php apache .htaccess mod-rewrite

我正在为我的 search.php 页面寻找正确的 htaccess 规则。因为我尝试了好几个,但没有任何效果。我现有的 htaccess 规则可能与之冲突。所以我也将我当前的 htaccess 文件粘贴到这里。目前我有以下带有查询字符串的搜索页面网址:

http://my-domain.com/search.php?q=keyword

我想要以下干净的网址:

http://my-domain.com/search/keyword

我的 HTML 表单是:

<form method="get" action="search.php">
    <input type="text" name="q" class="txtfield" value="<?php echo $q; ?>" placeholder="Search post here..." />
</form>

htaccess:

Options +FollowSymlinks -MultiViews

RewriteBase /

#Enable mod rewrite
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ page.php?category=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/([^/]+)$ page.php?category=$1&post=$2 [QSA,L]

请注意,我在上面的文件中没有与搜索相关的 htaccess 规则,因为正如我所说,没有任何效果。我总是看到404页面。这就是我删除它的原因。

此外,如果我将来将提交按钮放入表单中,如下所示:

<form method="get" action="search.php">
    <input type="text" name="q" class="txtfield" value="<?php echo $q; ?>" placeholder="Search post here..." />
    <input type="submit" name="btnsearch" class="btn" value="Search" />
</form>

我的搜索网址将更改为:

http://my-domain.com/search.php?q=keyword&btnsearch=Search

然后我想知道我必须在正确的 htaccess 规则中进行哪些纠正/修改,您将根据我的第一个查询向我提供该规则?如果搜索 url 包含两个或两个以上查询字符串,我们是否应该有两个规则?请帮助我解决这些问题。谢谢。

最佳答案

提交表单后,它确实会将您的 URL 设为:

http://my-domain.com/search.php?q=keyword&btnsearch=Search

为了保持提交的 URL 美观,您需要防止使用 Javascript 代码提交表单并使 URL 看起来像这样。

<html><head>
<script>
function prettySubmit(form, evt) {
   evt.preventDefault();
   window.location = form.action + '/' + form.q.value.replace(/ /g, '+');
   return false;
}    
</script>
</head><body>
<form method="get" action="search" onsubmit='return prettySubmit(this, event);'>
   <input type="text" name="q" value="<?php if (isset($_GET['q'])) echo $_GET['q']; ?>"
       class="txtfield" placeholder="Search post here..." />
   <input type="submit" name="btnsearch" class="btn" value="Search" />
</form>
</body></html>

这会将您的表单提交到漂亮的网址:

http://my-domain.com/search/keyword

现在您需要在根 .htaccess 中使用此规则来处理这个漂亮的 URL;

Options -MultiViews
RewriteEngine On

RewriteRule ^search/(.+)$ search.php?q=$1 [L,QSA,NC]

更新:

这是一种完全使用 mod_rewrite 规则而不使用任何 JS 来完成此操作的方法。在 root .htaccess 中使用以下规则:

Options -MultiViews
RewriteEngine On
RewriteBase /my-project/

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+) [NC]
RewriteRule ^ search/%1? [R=302,L,NE]

# internal forward from pretty URL to actual one         
RewriteRule ^search/(.+)$ search.php?q=$1 [L,QSA,NC]

关于php - search.php 页面的 htaccess 规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37247135/

相关文章:

apache - 将 www.subdomain.example.com 重定向到 subdomain.example.com

python - 使用 fastcgi 在共享主机上部署站点

php - CakePHP 数据库连接 "Mysql"丢失或无法创建。 core.php 和 bootstrap.php

php - 使用数据库信息更改文本的 CSS/外观

PHP代码没有被执行,但是代码显示在浏览器源代码中

php - codeigniter 总是显示 404 错误 ubuntu 14.04

wordpress - 404 是由于 WordPress mod_rewrite 规则造成的?

php - 加载页面时如何更改地址栏中的 URL?

php - 如何使 mysqli_query 比较变量相同?

PHP - 通往 MySQL 的 SSH 隧道