确保链接不在域根目录时的最佳方式/练习是否到达适当位置? [英] Best way/practice to ensure links are going to proper location when not on root of domain?

查看:103
本文介绍了确保链接不在域根目录时的最佳方式/练习是否到达适当位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道这一点,但是当你创建链接时(或者使用方法生成,或者在创建链接时),确保在Web应用程序(RoR,Sinatra,PHP,任何东西)中最好的方法是什么?无论你是否在域名的根目录): http:/ / /www.example.com/ http:/ /www.example.com/this/is/where/the/app/is/



我的想法是让最终用户指定一个文档根在你的应用程序的配置中的某处,并使用它,但是我试图想到一个很好的方式来做到这一点,而不需要最终用户配置任何东西。



<编辑:对于最终用户,我指的是在服务器上设置应用程序的人。



编辑:我可以使用开始的'/'来始终获取链接相关的领域,但问题是如果应用程序itse如果不是根源,但有一些地方像 http:// www.example.com/this/is/where/the/app/is/ ,所以我想说 gen_link('/')并让它返回 / this / is / where / the / app / is / gen_link('/ some / thing')和返回 / this / is / where / the / app / is / some / thing

解决方案 div>

如何在你的HTML布局的头部设置 base 元素?

首先,获取URL,例如。以Ilya的方式表示(如果PHP适合你)。之后,您可以使用 base 标签,如下所示:

 < base href =<?= $ full_site_url?> /> 

这将设置所有链接的默认URL,浏览器会将其预先添加到每个相关链接上该页面。


I've been wondering this for a while now, but what is the best way to ensure that in a web app (RoR, Sinatra, PHP, anything) that when you are creating links (either generating with a method, or writing in by hand) that they go to the proper place whether you are on the root of a domain or not: http://www.example.com/ or http://www.example.com/this/is/where/the/app/is/

My thoughts are get the end-user to specify a document root somewhere in the config of your app, and use that, however I'm trying to think of a nice way to do it without the end-user having to configure anything.

Edit: By end-user, I mean the person setting up the application on a server.

Edit: I can use the beginning '/' to always get the link relative to the domain, but the problem is what if the app itself is not at the root, but some place like http://www.example.com/this/is/where/the/app/is/ so i want to say gen_link('/') and have it return /this/is/where/the/app/is/ or gen_link('/some/thing') and return /this/is/where/the/app/is/some/thing

解决方案

How about trying to set the base element in the head of you html layout?
First, get the URL, eg. in a way Ilya suggests (if PHP is OK for you). After that you can use the base tag as follows:

<base href="<?= $full_site_url ?>" />

That will set the default URL for all the links and the browser will prepend it to every relative link on the page.

这篇关于确保链接不在域根目录时的最佳方式/练习是否到达适当位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆