【问题标题】:Zend/Apache mod_rewrite... what's wrong?Zend/Apache mod_rewrite...怎么了?
【发布时间】:2009-05-15 04:21:14
【问题描述】:

以下网址可以正常工作:

http://localhost/index/index/

但是,当它们像这样进来时,我无法 _get$ 变量:

http://localhost/index/index/test/1234/test2/4321

-但是-

但是,我可以通过以下方式 _get$ 变量:

http://localhost/index.php?test=1234&test2=4321
http://localhost/index?test=1234&test2=4321
http://localhost/index/index?test=1234&test2=4321

为什么当我使用 /index/index/var/val 方式时变量没有显示出来?

您将在下面找到我的 .htaccess 文件。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

【问题讨论】:

    标签: php zend-framework .htaccess mod-rewrite


    【解决方案1】:

    Zend Framework 不会将请求 uri 中的数据作为 $_GET 变量提供,要访问它们,请使用 控制器 中的键:

    $test = $this->getRequest()->getParam('test') //$test = 1234
    

    或者更短

    $test = $this->_getParam('test');
    

    【讨论】:

    • 这就是答案...我想我跑题了,因为我在 Zend_Filter_Input 中使用了 _GET$。
    【解决方案2】:

    因为$_GET 包含查询字符串中的变量 - 这是问号后面的 URL 部分。请注意,.htaccess 文件中的重写规则将不引用现有文件或目录的 所有 URL 仅转换为 index.php,而没有任何原始 URL 的痕迹(不过,正如 Gumbo 的评论所提醒的那样我,它仍然可以通过$_SERVER['REQUEST_URI'] 访问。您的RewriteRules 不会创建查询字符串(即他们不会在URL 中添加问号),这是您使用@987654326 需要做的@。

    我建议用类似的东西替换你最后的RewriteRule

    RewriteRule ^.*$ index.php$0 [NC,L]
    

    $0 会将原始 URL 附加到 index.php - 例如,http://localhost/index/index/test/1234/test2/4321 将变为 http://localhost/index.php/index/index/test/1234/test2/4321 然后请求将由 index.php 处理,$_SERVER['PATH_INFO'] 变量将设置为原始 URL,/index/index/test/1234/test2/4321。你可以编写一些 PHP 代码来解析它并选择你想要的任何参数。

    如果您不希望将开头的 /index/index 保存在 path_info 变量中,您可以像这样使用 RewriteRule

    RewriteRule ^/index/index(.*)$ index.php$1 [NC,L]
    

    RewriteRule ^(/index)*(.*)$ index.php$2 [NC,L]
    

    去除任意数量的前导/indexes。

    编辑:实际上,您可以保留现有的RewriteRules,只需查看$_SERVER['REQUEST_URI'] 即可获取原始请求URI;不需要弄乱路径信息。然后你可以在 PHP 中随意拆分。

    【讨论】:

    • 那么我需要做什么才能将查询字符串的其余部分转换为使用斜杠而不是 ?和 & 的?
    • 原始请求的 URL 路径和查询没有丢失(参见 $_SERVER['REQUEST_URI'])。
    • 似乎我的问题在于我对 Zend 缺乏了解,而不是我对 mod_rewrite 缺乏了解,哈哈。不过,请 Ty 帮忙。
    • 嗯,嗯...我写了我的答案,假设您使用的是纯 PHP,没有框架。哎呀我猜:-/
    猜你喜欢
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多