Web开发中如何有效管理根相对url
我正在设计一个网络应用程序,其中我将在 http://www.site.com/resources/ 上获取资源 目录
在我的 HTML 上,我会通过绝对路径 /resources/ 引用该目录,但问题是,当我在 Web 服务器中测试该目录时,路径现在将是 http://localhost/webapp/resources/ 并使用绝对路径会查找 //localhost/resources/
我发现可以使用./来引用webapp相对根目录,并且它有效。但我不喜欢它的样子。
我还有哪些其他选择?
- 使用绝对路径并将应用程序放置在服务器的 ROOT 目录中
- 使用 PHP / JSP 变量来确定当前根目录
- 继续使用 ./
- 其他内容...
提前致谢!
I am designing a webapp in which I will have my resources on the http://www.site.com/resources/ directory
On my HTMLs, I would refer to that directory by an absolute path /resources/, but the problem is that when I test that in a web server, the path now will be http://localhost/webapp/resources/ and using the absolute path will look for //localhost/resources/
I found that I can use ./ to refer to the webapp relative root directory, and it works. But I don't like how it looks.
Which other alternatives do I have?
- Use absolute path and place application in the server's ROOT directory
- Use a PHP / JSP variable that determines the current root directory
- Continue using ./
- Other thing...
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了重新表述您的问题,您的网站必须在两种不同的配置中工作:
http://servername/mypage.htm
和http://servername/SubSite/mypage.htm
第二个配置可能代表一个测试站点(甚至可能是开发人员计算机上的本地主机)。
在任一部署上,图像、链接、样式表的所有路径都必须使用相对寻址。
因此,设置图像的 src="/resources/mypic.jpg" 将使 SubSite 示例(在 IIS 中称为虚拟目录或应用程序)上的测试失败,因为 / 会告诉它查看
http://servername /resources
而不是http://servername/SubSite/resources
因此它不起作用。我对我的开发团队的立场是始终使用文档相对路径。
因此,在位于网站树根部的 mypage.htm 的基本示例中(无论它位于文件系统或 Web 服务器的哪个位置),您可以设置图像的 src="./resources/mypic.jpg"
基本上,请记住,在 html 中,当您看到“./”时,它表示来自此页面。如果您需要进入一个目录(假设您在 ~/Reports/myreport.htm 下有另一个页面也需要显示该图像,您可以将该页面上的图像标记上的源设置为 src="../resources /mypic.jpg”,这意味着上升一级。
注意,我在那里做了一些偷偷摸摸的事情。在 ASP.NET 中,~/ 表示网站的根目录,无论它位于何处。我用它作为速记来反映的根尽管在 ASP.NET 中,任何服务器端控件实际上都可以使用前面带有 ~/ 的 URL,并且它会正确地将其解析为正确的路径
。研究这个问题的其他部分,如果您使用文档相对路径来指向图像等,那么该路径是相对于 CSS 文件的,这是一件好事,因为 CSS 文件很少改变相对于图像的位置。 CSS 文件叫什么。然而,文件确实会移动,这让我想到了
我到目前为止所解释的内容对于基本网页非常有用,并且使它们非常可移植。一旦您在不同的目录中有多个页面引用生成对 CSS、图像等的 HTML 引用的代码或控件,您也需要修复这些路径。
基本上,您需要修改这些 URL(通常在您编写的代码或控件内),以获得所需的正确 ./ 或 ../ 来获取您想要的资源。
在 ASP 中,您可以使用 VBscript 来使用 Server.MapPath("/resources/mypic.jpg") 来获取相对于站点根目录的正确 URL。
在 ASP.NET 中,您可以使用 Page.MapPath("~/resources/mypic.jpg") 来获取所需的正确 URL。
我还编写了函数来生成 .NET 中所需的正确 ./ 或 ../ 削减,如下所示:
您可以通过 converter.telerik.com 转换为 VB.net。我将我的母版页设置为使用该函数公开 javascript 全局变量 JSPATHTOSITEROOT,这样我也可以通过 JS 为我需要的任何路径添加前缀。
对于 PHP,我已经有一段时间没有接触过它了,所以我所能给出的只是我发布的其他代码的一些指针,以类似地解决这个问题:
https://www.php.net/manual/en/function。 dirname.php#91208
该链接展示了如何使 PHP 包含嵌套工作像 C/C++ 包含嵌套一样,但您也可以使用相同的技巧来修复 html 的路径。
我的最终总结是,如果有人向您提供一个项目,其中的图像和 CSS 路径全部以“/”开头,那么他们将无法通过测试。该代码不可移植。 21 世纪的优秀 Web 开发人员都知道,由于各种原因和适当的代码,站点可能会部署在服务器根目录或作为子站点。
To rephrase your problem, you have a site that will must work in two different configurations:
http://servername/mypage.htm
, andhttp://servername/SubSite/mypage.htm
The second configuration might represent a test site (possibly even localhost on the developer's machine).
All paths for images, links, stylesheets must work using relative addressing, on either deployment.
Thus, setting an image's src="/resources/mypic.jpg" would fail the test on the SubSite example (called a Virtual Directory or Application in IIS) because the / would tell it to look at
http://servername/resources
instead ofhttp://servername/SubSite/resources
and so it doesn't work.My stance for my dev team is to ALWAYS use document-relative pathing.
Thus, in the basic example of mypage.htm which sits at the root of the web site tree (regardless of where it sits on the file system or web server) you would set the image's src="./resources/mypic.jpg"
Basically, remember, in html, when you see "./" it means from this page. If you need to go up a directory (say you have another page under ~/Reports/myreport.htm that also needs to show that image, you would set the source on the image tag on that page as src="../resources/mypic.jpg" which means go up one level.
Note, I did something sneaky there. in ASP.NET, ~/ means the root of the web site, regardless of where it resides. I'm using it as shorthand to reflect the root of the site. Though in ASP.NET, any server-side control can actually use a URL with ~/ in front of it and it will correctly resolve it to the proper pathing.
I'll tack in another important point I just learned today while researching other parts of this problem. In a CSS file, if you use document-relative paths to point to images, etc, that pathing is relative to the CSS file. That's a good thing, because CSS files seldom change position, relative to image files. What call's the CSS file does move around however, which brings me to my next point.
What I've explained so far works great for basic web pages and makes them very portable. Once you have multiple pages in different directories referring to code or controls that generate HTML references to CSS, images, etc, you need to get THOSE paths fixed as well.
Basically, you need to modify those URLs (usually within the code or controls you wrote) to have the proper ./ or ../ needed to get to the resource you meant.
In ASP, you could use VBscript to use Server.MapPath("/resources/mypic.jpg") to get the proper URL you need relative to the site's root.
In ASP.NET, you can use Page.MapPath("~/resources/mypic.jpg") to get the proper URL you need.
I've also written functions to generate just the correct ./ or ../ slashing I need in .NET as follows:
You can run that through converter.telerik.com to convert to VB.net if need be. I set my master pages to expose a javascript global variable JSPATHTOSITEROOT using that function so I can prefix any paths I need via JS as well.
For PHP, I haven't touched that in a while, so all I can give is some pointers to other code I've published to solve this similarly:
https://www.php.net/manual/en/function.dirname.php#91208
That link shows how to make PHP include nesting work like C/C++ include nesting, but you can use the same trick to fix up your paths for html as well.
My end summary is, if anybody hands you a project with paths for images and css that all start with "/" they fail the test. The code is not portable. A good web developer in the 21st century is aware that a site may be deployed at the server root or as a sub-site for a variety of reasons and codes appropriately.