【发布时间】:2013-07-07 21:04:01
【问题描述】:
所以我尝试在 GET 表单生成的 URL 上使用 mod_rewrite 来重定向它:
index.php?page=trade&stocksymbol=GOOG
到这里:
trade/GOOG
我的 .htaccess 中已经有这一行(Multiviews 已关闭):
RewriteRule ^trade$ index.php?page=trade [L]
RewriteRule ^trade/(.*)$ index.php?page=trade&stocksymbol=$1 [L]
如果我在地址栏中手动输入 trade/GOOG/,它就可以正常工作。
问题是我使用的是 MVC 模型,其中控制器 index.php 呈现页眉 + 页脚以及视图 trade.php 和表单本身在 trade.php 上。我想让表单提交并最终在 trade/GOOG - 但由于它是一个 GET 表单,而是转到 trade/trade?stocksymbol=GOOG。
trade.php
<form action="trade" method="get">
<input type="text" name="stocksymbol" size="10" />
<input type="submit" value="Search" />
</form>
index.php
if (isset($_GET['page']))
$page = htmlspecialchars($_GET['page']);
else
$page = 'index';
switch ($page)
{
case 'trade':
if (isset($_GET['stocksymbol']))
{
$stocksymbol = htmlspecialchars($_GET['stocksymbol']);
}
render('templates/header', array('title' => 'Trade')); //render() is a function that extracts the array elements into variables and spits out the HTML
render('trade', array('page' => $page, 'stocksymbol' => $stocksymbol));
render('templates/footer');
break;
...
我知道我在这里犯了一些新手错误,因为我不太了解 mod_rewrite 并且逻辑有问题,但我还不能解决这个问题。如果可能的话,为了简单起见,我想只使用 mod_rewrite 而不是 Javascript 解决方案来解决这个问题,因为我想知道 mod_rewrite 是否可行。
编辑:
原来我的 GET 请求没有通过。通过查看index.php中的$_SERVER['REQUEST_URI'],我发现正在发送的请求是/trade?stocksymbol=GOOG(表明index.php没有收到@之后的内容987654329@)。
另外,打开Chrome中的开发者工具我可以看到每次确实有一个GET请求,但是if (isset($_GET['stocksymbol']))不管是在index.php还是中都是假的trade.php.
所以我认为 mod_rewrite 搞砸了我的 GET 请求。
【问题讨论】:
标签: php .htaccess mod-rewrite get