[英]How to manipulate the URL from example.com/search/?s=some to example.com/search/some
I have a search form on my website. 我的网站上有搜索表。 When submitting the form, it is processed by my index.php, so the URL would be like index.php?s=some.
提交表单时,它由我的index.php处理,因此URL类似于index.php?s = some。 Now I added a few rewrite rules in my htaccess:
现在,我在htaccess中添加了一些重写规则:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/$ index.php?s=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?s=$2&f=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)?$ index.php?s=$2&f=$1&t=$3 [QSA,L]
My form has the action "search", it looks like this: 我的表单具有动作“搜索”,它看起来像这样:
<form id="searchform" method="get" action="search">
Now what happens when submitting is that you get redirected to example.com/search/?s=some But what I want is to be redirected to example.com/search/some What could I add to my rewrite rules to achieve this? 现在,提交时发生的事情是您被重定向到example.com/search/?s=some。但是我想要的是重定向到example.com/search/some。我可以在重写规则中添加些什么来实现这一点?
I came across http://matthewjamestaylor.com/blog/how-to-post-forms-to-clean-rewritten-urls when I was looking for a solution for a project of mine. 在寻找我的项目解决方案时,我遇到了http://matthewjamestaylor.com/blog/how-to-post-forms-to-clean-rewrite-urls 。 The PHP involved can be a little overkill, but it does have a lot of utility.
涉及的PHP可能有点过大,但确实有很多实用程序。
.htaccess .htaccess
# Clean up search URL
RewriteRule ^search/([^/\.]+)$ search.php?q=$1
PHP 的PHP
<?
// Collect the search phrase from the URL
$qs = mysql_real_escape_string($_GET['q']);
// Clean up by removing unwanted characters
$qsclean = ereg_replace("[^ 0-9a-zA-Z]", " ", $qs);
// Remove multiple adjacent spaces
while (strstr($qsclean, " ")) {
$qsclean = str_replace(" ", " ", $qsclean);
}
// Prefix each keyword with a + (for the SQL statement)
$qsql = "+".str_replace(" ", " +", $qsclean);
?>
Why dont you use the post method ? 为什么不使用post方法? I thing this method make more complex programming Please use the post method.
我认为此方法会使编程更加复杂,请使用post方法。 it is more secure.
它更安全。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.