【问题标题】:URL rewrite with form and 3 parameters使用表单和 3 个参数重写 URL
【发布时间】:2012-07-14 16:14:31
【问题描述】:

我有一个带有 3 个参数的表单的网页,例如

http://www.likeforex.com/currency-converter/fxconverter.php?f=euro-eur&t=usd-us-dollar&amt=1

我使用 .htaccess 将页面重写为:f_t.htm/amt 为:

http://www.likeforex.com/currency-converter/euro-eur_usd-us-dollar.htm/1

但是,我的问题是当用户更改amt(页面中间)的值时,表单被重新提交,重新提交的页面仍然是参数格式。 url如何改写成后格式?

提交按钮有什么问题,还是其他什么问题?

the form:
<form name="e" method="get" action="fxconverter.php">
the amt parameter:
<input type="text" id="amt" name="amt" size="16" maxlength="16" value="'.$amt.'" onclick="this.focus();this.select();" />'
the submit button:
<input type="submit" value="Convert" class="bt" />
the .htaccess line: 
RewriteRule ^(.*)_([^_]+)\.htm/([^/]+)$ fxconverter.php?f=$1&t=$2&amt=$3 [NC]

非常感谢。

【问题讨论】:

  • 能不能给我们看一下表格和相关的mod_rewrite,不然就是猜谜游戏了……
  • 没什么。只有 RewriteRule ^(.*)_([^_]+)\.htm/([^/]+)$ fxconverter.php?f=$1&t=$2&amt=$3 [NC]
  • @likeforex.com 请更新您的问题。

标签: php .htaccess


【解决方案1】:

无论POST/GET如何,您都不能使用表单并期望参数像url一样输出,例如http://www.likeforex.com/currency-converter/euro-eur_usd-us-dollar.htm/1

您需要做的是稍微更改表单并使用 javascript 构建 url 然后重定向。

<script>
function goForm(form){
    f = form.elements["f"].value;
    t = form.elements["t"].value;
    amt = form.elements["amt"].value;

    window.location.href = 'http://www.likeforex.com/currency-converter/'+f+'_'+t+'.htm/'+amt;
}
</script>

<form name="currencyForm" method="GET" action="">
<input id="f" name="f" value="euro-eur" type="hidden">
<input id="t" name="t" value="usd-us-dollar" type="hidden">
<input id="amt" name="amt" size="16" maxlength="16" value="1" onclick="this.focus();this.select();" type="text">
<input type="button" name="button" value="Click" onClick="goForm(this.form)">
</form>

然后你可以利用 mod_rewrite。

虽然我个人会废弃GET(用于花哨的网址),而只使用POST。搜索引擎不会查询您的表单,因此仅使用 GET 没有任何优势,因为它可能会让想要抓取您的内容以消化其工作原理的人更容易。还要记住,没有启用 javascript 的任何人都无法在我的解决方案中使用该表单。

【讨论】:

  • 非常感谢。没有 JS 就没有其他解决方案?
猜你喜欢
  • 2013-08-30
  • 1970-01-01
  • 2016-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-12
相关资源
最近更新 更多