http://search.php页面的访问规则
我正在为我的search.php页面寻找正确的htaccess规则。因为我试了好几次,但都没用。我现有的htaccess规则可能与之冲突。因此,我也将当前的htaccess文件粘贴到这里。目前我有以下带有查询字符串的搜索页面url:http://search.php页面的访问规则,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,我正在为我的search.php页面寻找正确的htaccess规则。因为我试了好几次,但都没用。我现有的htaccess规则可能与之冲突。因此,我也将当前的htaccess文件粘贴到这里。目前我有以下带有查询字符串的搜索页面url: http://my-domain.com/search.php?q=keyword 我想要以下干净的url: http://my-domain.com/search/keyword 我的HTML表格是: <form method="get" action=
http://my-domain.com/search.php?q=keyword
我想要以下干净的url:
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规则,因为正如我所说的,并没有任何效果。我总是看到404页。这就是我把它拿走的原因
另外,如果我在将来的某个时候将submit按钮放在表单中,如下所示:
<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>
然后,我想知道,根据我的第一个查询,您将给我的正确htaccess规则,我需要做哪些更正/修改?如果搜索url包含两个或两个以上的查询字符串,我们应该有两个规则吗?请帮我搞定这家伙。谢谢。1.删除.php扩展名(可选) 2.支持<代码>http://my-domain.com/find/keyword
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^find/([^/\.]+)$ /search.php?keyword=$1
3.测试
<?php
if ($_POST['query'] != '') {
header( 'Location: /find/' . $_POST['query']);
}
echo $_GET['keyword'];
?>
<form method="post" action="">
<fieldset>
<input type="text" name="query" />
<input type="submit" value="Search" />
</fieldset>
</form>
希望这有帮助。提交表单时,确实会将您的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>
现在,您需要在root.htaccess中使用此规则来处理这个漂亮的URL
Options -MultiViews
RewriteEngine On
RewriteRule ^search/(.+)$ search.php?q=$1 [L,QSA,NC]
更新:
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]
下面是一种完全使用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]
find.php
是如何出现的?OP显示文件名为search.php
…因为search/keyword
中的名称search
不应该与文件名search.php相同,所以我给了它另一个名称。它当然不应该。。。检查你的日志。然后关闭多视图
。我将搜索表单操作更改为搜索
,并将第2点htaccess规则放在我的htaccess文件中,但它现在在地址栏中显示/search/?q=关键字
。如何删除?q=
?我认为您的第一条规则不是强制性的。我们可以简单地改变表单的action属性。不是吗?好吧,但我不明白为什么每个人都建议使用JavaScript?为什么不能只在PHP/htaccess中完成?如果有人关闭了浏览器中的JS,会发生什么?你能告诉我没有JS怎么做吗?我想要一个在这两种情况下都能工作的解决方案-一个没有提交按钮的表单和一个有提交按钮的表单。我需要两种情况下的访问规则。。那是干什么的?好的,谢谢。是的,我查过了。事实上,我用js尝试了你的第一种方法。@anubhava先生,你是一个真正的.htaccess天才。几个小时来我一直在为此拼命。
http://my-domain.com/search/keyword
Options -MultiViews
RewriteEngine On
RewriteRule ^search/(.+)$ search.php?q=$1 [L,QSA,NC]
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]