duansengcha9114 2019-03-01 22:36
浏览 41

使用PHP清理GET查询URL

So I've used mod_rewrite to rewrite my get query urls from /news?news_id=6 to /news/6. But now I'd like to be able to also do the same for when somebody submits a form using a get method. Right now when you submit a form it still directs you to /news/news_id=6 instead of /news/6, how would I be able to clean the get query url? I'm pretty sure this can't be done using mod_rewrite (please correct me if you can do so), so I imagine you'd need to use either Javascript or PHP. I've been trying to do this with PHP and so far I've done as follows:

 if (!empty($_GET['news_id'])) {
    $redirecturl = "/news/" . $_GET['news_id'];
    $redirecturl = str_replace(" ", "-", $redirecturl);
    header('Location: ' . $redirecturl, true, 303);
    die();
}

However this results in an error on the browser which says "localhost redirected you too many times." How would I go about achieving this task with PHP?

Thank you.

  • 写回答

1条回答 默认 最新

  • duanli8577 2019-03-01 23:15
    关注

    Not sure but, does it have to be a 'get' form? If you want a clean URL this would be a very simple example with the post method (without any security checks!). You can leave the 'action' empty if it's the same page:

    Form code:

    <form action="" method="post">
        [Other form code ]
        <input type="hidden" name="news_id" value="6" />
    </form>
    

    Process code:

    if ($_POST && !empty($_POST['news_id'])) {
        // Do what you want with: $_POST['news_id'];
    }
    

    Also, you could make a new mod_rewrite where you handle your form processing, like: /news6/process for example. This would be better for security reasons and you will have much cleaner code. After you have done your processing set a session variable to let the user know if the processing was completed or not on '/news/6'.

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?