ds34222222 2011-10-04 20:27
浏览 27
已采纳

使用“干净网址”时使用相对网址?

I direct all url requests to a front controller. The url requests look something like example.com/controller/action/etc. The problem is, using relative urls within the program. This works fine if there is only one slash in the url request, but if there is more than one slash, the relative url becomes broken.

At location example.com/controller, relative url other-controller works fine taking the user to example.com/other-controller.

At location example.com/controller/action, relative url other-controller doesn't work taking the user to example.com/controller/other-controller.

Any recommendations on how to get around this? Hoping I can do this without using absolute paths or some url generating code everytime I want to make a url in the app.

In case it matters, I am currently using this .htaccess code to redirect incomming requests to the front controller:

# 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>
  • 写回答

1条回答 默认 最新

  • doujiayuan8415 2011-10-04 20:38
    关注

    You're going to need to use absolute URLs to do what you want without looking at the current path you are at and adding the proper number of ..s. You don't need to have the domain for each one, you can just use URLs like /controller/action and /other-controller/action. These will work for both controllers seamlessly. A sample anchor tag:

    <a href="/controller/action">Do something!</a>
    

    As a side note, oftentimes, people will write functions to generate URLs. If you are using a MVC framework, it may be in the form of adding a method to a model and call the function get_absolute_url which returns the form I mentioned above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看