mod_rewrite and GET value + MVC


mod_rewrite and GET value + MVC

所以我试图在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。phprade.php上。我想让表单提交并最终位于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_rerite是否可行。

编辑:

结果我的GET请求没有通过。通过查看index.php中的$_SERVER['REQUEST_URI'],我发现正在发送的请求是/trade?stocksymbol=GOOG(这表明index.php没有接收到?之后的内容)。

此外,打开Chrome中的开发人员工具,我可以看到每次确实有一个GET请求,但无论它是在index.php还是trade.php中,if (isset($_GET['stocksymbol']))都是错误的。

所以我认为mod_rewrite把我的GET请求搞砸了。

您可以使用mod_rewrite或在index.php中执行此操作。在php中,您只需要检查是否直接为/index.php?page=trade发出请求,如果是,则进行外部重定向。这将取决于您的代码以及您想要在哪里实现它,但有一个地方您可以将它放在'trade'case中:

case 'trade':
    if (isset($_GET['stocksymbol']))
    {
        $stocksymbol = htmlspecialchars($_GET['stocksymbol']);
        // stuff here to redirect browser
        $self_uri = "/index.php?page=trade";
        if (strncmp($_SERVER['REQUEST_URI'], $self_uri, strlen($self_uri)))
        {
            // if the requested URI starts with /index.php?page=trade, redirect
            header('HTTP/1.1 301 Moved Permanently');
            header('Location: /trade/' . $stocksymbol);
            exit();
        }
        // end browser redirect
    }
    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的方法也是一样的。您正在检查是否为/index.php?page=trade发出了实际请求(在这种情况下不是URI,因为重写引擎对此进行了更改),然后重定向浏览器:

RewriteCond %{THE_REQUEST} ^(GET|HEAD)' /index'.php'?page=trade&stocksymbol=([^&' ]+)
RewriteRule ^ /trade/%2? [L,R=301]

经过几个小时的搜寻,我终于找到了答案,它比我想象的要简单得多。。。

我所要做的就是改变

RewriteRule ^trade$ index.php?page=trade [L]

RewriteRule ^trade$ index.php?page=trade [qsappend]

以下是我找到的解释:http://help.sap.com/saphelp_nwpi711/helpdata/en/48/9266f7aa6b17cee10000000a421937/content.htm

为转移注意力向林道歉——原来这是我知识上的差距,而不是更神秘的东西。谢谢你帮忙。