繁体   English   中英

如何处理从example.com/search/?s=some到example.com/search/some的URL

[英]How to manipulate the URL from example.com/search/?s=some to example.com/search/some

我的网站上有搜索表。 提交表单时,它由我的index.php处理,因此URL类似于index.php?s = some。 现在,我在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]

我的表单具有动作“搜索”,它看起来像这样:

<form id="searchform" method="get" action="search">

现在,提交时发生的事情是您被重定向到example.com/search/?s=some。但是我想要的是重定向到example.com/search/some。我可以在重写规则中添加些什么来实现这一点?

在寻找我的项目解决方案时,我遇到了http://matthewjamestaylor.com/blog/how-to-post-forms-to-clean-rewrite-urls 涉及的PHP可能有点过大,但确实有很多实用程序。

.htaccess

# Clean up search URL
RewriteRule ^search/([^/\.]+)$ search.php?q=$1

的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);
?>

为什么不使用post方法? 我认为此方法会使编程更加复杂,请使用post方法。 它更安全。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM