【问题标题】:Zend Framework 2 URL RewritesZend Framework 2 URL 重写
【发布时间】:2014-03-11 21:54:34
【问题描述】:

我正在使用 Zend Framework 2 和随附的 .htaccess 进行 URL 重写,如下所示:

RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

此外,我还定义了如下路由:/customers/8080/edit。现在我需要这条路线也可以通过/customers/edit/8080 访问。额外的 URL 应该是一个别名而不是一个简单的重定向。

因为我的路由已经非常复杂,所以在module.config.php 中重写我的路由确实是没有选择的。我认为实现这一点的最简单方法是在我的.htaccess 中添加重写条件并定义路由/customers/edit/8080 只需映射到/customers/edit/8080

由于 ZF 将每个 URL 重定向到 index.php,我一直在定义我的别名路由。

任何建议都会很棒(:

【问题讨论】:

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


    【解决方案1】:

    我正在处理一个非常相似的问题,但我继承了 Zend 安装,所以我不知道“开箱即用”安装是什么样的。

    我(在某种程度上)通过在application.ini 文件中添加“baseURL”参数解决了我的问题。在这里查看我的问题和解决方案,它可能会有所帮助。 Moving Zend Framework site to new host - "An error occurred Page not found"

    注意:我对我的结果不是 100% 满意。我仍在努力,但我已经接近了。

    【讨论】:

    • 感谢您的提示 - 我会看看您的解决方案。
    【解决方案2】:

    通过简单地将文字 RewriteRule 添加到 .htaccess 文件来解决此问题。这就是我最初试图让这个工作的原因,但我并没有在我的模式部分省略前导斜杠。

    这就是我想出的:

    RewriteRule ^customers/([0-9]+)/base/edit/([0-9]+)/?$ /customers/$1/edit/base/$2 [NC,L,P]
    

    考虑指定[P]标签,如果不应该更改url,它会将请求作为代理请求处理。

    另见this question.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-19
      • 2010-10-12
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      相关资源
      最近更新 更多