开发者

Using relative urls when using "clean urls"?

开发者 https://www.devze.com 2023-04-10 10:22 出处:网络
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 ther

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 abs开发者_如何转开发olute 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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号