我将所有 url 请求定向到前端控制器。url 请求看起来像example.com/controller/action/etc
. 问题是,在程序中使用相对 url。如果 url 请求中只有一个斜杠,这可以正常工作,但如果有多个斜杠,则相对 url 会损坏。
在位置example.com/controller
,相对 urlother-controller
可以很好地将用户带到example.com/other-controller
。
在 location example.com/controller/action
,相对 urlother-controller
无法将用户带到example.com/controller/other-controller
.
关于如何解决这个问题的任何建议?希望每次我想在应用程序中创建一个 url 时,我都可以在不使用绝对路径或一些 url 生成代码的情况下做到这一点。
万一这很重要,我目前正在使用这个 .htaccess 代码将传入的请求重定向到前端控制器:
# Point all to index.php except existing files
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>