![](/img/trans.png)
[英]How do I rewrite the URL for example.com/index.php#anchor to example.com/anchor?
[英]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.