【问题标题】:How to submit form method get with mod_rewrite url?如何使用 mod_rewrite url 提交表单方法获取?
【发布时间】:2017-03-01 22:24:45
【问题描述】:

如何使用mod_rewrite url 提交表单方法get

这是我的代码

<form method="GET" action="https://www.example.com/search/">
    <input type="text" name="search" value="" placeholder="search">
    <input type="submit" value="OK">
</form>

当我提交表单时。它将被重定向到

https://www.example.com/search/?search=test

如何应用我的代码重定向到

https://www.example.com/search/test/1

提交表单后?

【问题讨论】:

  • 方法一:用POST代替GET方法2:使用javascript/jquery提交表单,在这里你可以从表单元素构建你的url

标签: php html forms apache mod-rewrite


【解决方案1】:

你的后端应该设置Location header:

Location: https://your.domain/search/test/1

The code:

$ cat search/index.html
<form method="GET" action="https://your.domain/search/post.php">
    <input type="text" name="search" value="" placeholder="search">
    <input type="submit" value="OK">
</form>


$ cat search/post.php
<?php
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: https://your.domain/search/test.html");
    exit();
?> 

$ cat search/test.html
Redirection works fine

要动态决定重定向到哪里,您应该检查$POST[ ] hash。

【讨论】:

  • 是的,您还应该检查是否没有 POST/GET 数据,您显示您的表单否则重定向。
  • 我不明白你能给我一些示例代码吗?
  • 这是 php 吗? $ cat search/post.php 意味着什么?
  • @RobertDown 那是三个不同的文件。 cat 是 unix 命令; $ 是 shell 提示符
猜你喜欢
  • 2018-04-13
  • 1970-01-01
  • 2021-06-08
  • 1970-01-01
  • 2018-03-18
  • 2022-10-05
  • 2021-08-08
  • 2011-05-02
  • 1970-01-01
相关资源
最近更新 更多