【发布时间】:2011-10-04 20:27:58
【问题描述】:
我将所有 url 请求定向到前端控制器。 url 请求类似于example.com/controller/action/etc。问题是,在程序中使用相对 url。如果 url 请求中只有一个斜杠,这可以正常工作,但如果有多个斜杠,则相对 url 会损坏。
在位置example.com/controller,相对网址other-controller 可以很好地将用户带到example.com/other-controller。
在位置example.com/controller/action,相对网址other-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>
【问题讨论】:
标签: php relative-path clean-urls