重写URL时,一个表单提交 [英] rewrite url when a form is submitted

查看:142
本文介绍了重写URL时,一个表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于使用htaccess的重定向问题。在一个GET搜索表单提交,我得到以下网址:

<$p$p><$c$c>http://www.example.com/search/?criteria=foo&filter=all&date=all&submit=search&page=1

以上工作正常。然后,我有这样的重写规则在我的htaccess的:

 重写规则^ / / / / / $的search.php标准= $ 2安培(*)(*)(*)(*)(*);?过滤器= $ 3及日期= $ 4和提交= 1 $&安培;页面= $ 5 L]
 

以上也能正常工作。它可以让我用下面的结构我的搜索结果:

  http://www.example.com/search/foo/title/all/1/
 

现在我的问题是,当我点击提交我想它使用新推行清洁URL结构但是它采用了混乱之一,问号和等号。现在,自动,我想,也许我已经做在PHP中的重定向但是如果我能与htaccess的我会更开心,因为它意味着更清洁的code做到这一点。

我也明白,我可以通过截获的点击和创建搜索引擎友好的URL,但再次,如果他们的是一种与htaccess的我想preFER认为做到这一点的JavaScript。

希望你明白我尝试实现和许多感谢您的阅读,

fl3x7

PS IM pretty的新htaccess的,所以如果你能解释/指导等,这将是巨大的。

解决方案

您无法使用的.htaccess这样做完全。

您可以使用JavaScript(拦截onclick事件,使用用户输入的数据然后重定向页面到这个网址构造URL字符串)做到这一点,但是这是不是真的可靠考虑的JavaScript运行在客户端。

我的建议是使用PHP这一点。这个过程是一样的,但我会使用POST而不是GET作为形式的方法。之前的页面被渲染,检测 $ _ POST 数据和构建 $网址使用它。然后页面重定向到 $网址,使用标题(位置:$网址); 这将是最好使用退出(); 在此之后,以确保它下面的code不会得到执行,因为重定向是由

Question regarding redirection using htaccess. On a GET search form submit I get the following url:

http://www.example.com/search/?criteria=foo&filter=all&date=all&submit=search&page=1

The above works fine. Then I have this rewrite rule in my htaccess:

RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/$ search.php?criteria=$2&filter=$3&date=$4&submit=$1&page=$5 [L]

The above also works fine. It allows me to use the following structure for my search results:

http://www.example.com/search/foo/title/all/1/

Now my question is when I click submit I would like it to use the new clearner url structure however it uses the messier one with the question marks and the equal signs. Now automatically I was thinking maybe I have to do a redirect in PHP however if I can do it with htaccess I would be happier as it means cleaner code.

I also understand I can do this with JavaScript by intercepting the click and creating the seo friendly url but again if their is a way with htaccess I would prefer that.

Hope you understand what im trying to achieve and many thanks for reading,

fl3x7

p.s im pretty new to htaccess so if you can explain/ guide etc that would be great

解决方案

You cannot do it solely with .htaccess.

You can do it with javascript (intercept the onclick event, construct the url string using the user entered data then redirect the page to this url), but this is not really reliable considering that javascript runs on the client side.

My suggestion is to use PHP for this. The process is the same, but I'll use POST instead of GET as a method for the form. Before the page gets rendered, detect the $_POST data and construct the $url using it. Then redirect the page to that $url, using header("Location: ".$url); It will be best to use exit(); after this, to make sure that the code below it will not get executed as the redirect is made.

这篇关于重写URL时,一个表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆