【问题标题】:PHP search form not working with .htaccessPHP 搜索表单不适用于 .htaccess
【发布时间】:2017-12-25 08:43:22
【问题描述】:

我有带代码的搜索国家/地区表单:

 <form name="form_search" id="form_search" class="form1" method="get" action="country/search"><div class="row">
<div class="col-sm-4 col-md-7">
<label>Select country</label>
<div class="select1_inner">
<div class="form-group">
<select class="select2 select" style="width: 100%" name="q" id="q">
<?php
$sql = "SELECT * FROM country";
$query = $conn->query($sql);
while ($row = $query->fetch_assoc()) :
$country_name=$row['country_name']; 
?>
<option value=<?php echo $country_name?>><?php echo $country_name?></option>
<?php endwhile;?> 
</select>
</div>
</div>
</div>

在前端url 将显示:http://example.com/country/search?q=parameter

但我会将url 重定向到后端的真实目录http://example.com/page.php?module=search&id1=$1

使用 .htaccess

 RewriteEngine On
 Options -Indexes
 rewriteRule ^country/search$  page.php?module=search&id1=$1  [L]

但这不起作用我无法获得价值q 参数

请帮助我如何为这个表单配置 .htaccess

谢谢。

【问题讨论】:

    标签: php forms .htaccess


    【解决方案1】:

    您应该使用RewriteCond 反向引用:

    RewriteCond %{QUERY_STRING} q=([^&]*)
    RewriteRule ^country/search$ page.php?module=search&id1=%1 [R=301,L]
    

    第 1 行:测试查询字符串是否包含q={any_characters}

    第 2 行:如果 url 是 country/search,则永久重定向到 page.php?module=search&amp;id1={any_characters}

    【讨论】:

    猜你喜欢
    • 2015-12-09
    • 1970-01-01
    • 2015-01-22
    • 2018-05-16
    • 1970-01-01
    • 2014-11-10
    • 2014-11-21
    • 2017-01-23
    • 1970-01-01
    相关资源
    最近更新 更多