开发者

path question for creating html file under ~/public_html

开发者 https://www.devze.com 2022-12-18 12:29 出处:网络
I am writing a html file under~/public_html on our server. I would like to refer to other files under ~/public_html in the html file. I now only know how to specify the path to those files (like pictu

I am writing a html file under ~/public_html on our server. I would like to refer to other files under ~/public_html in the html file. I now only know how to specify the path to those files (like pictures) via relative path from the html file. But I would like to know if there is a way to specify the absolute path? Becau开发者_JAVA技巧se sometimes I will move the html file under ~/public_html, so the relative path from the html file to those files might not hold any longer.

Thanks and regards!


Update:

Thanks! I still not quite get how to do it. For example,

if on the linux server, I have ~/public_html/doc/doc1.html and ~/public_html/pic/pic1.jpg. I would like to refer to pic1.jpg in the doc1.html. I can just write "../pic/pic1.jpg". How I can access doc1.html from outside via link www.xxx.xxx/~tim/doc/doc1.html and the picture can be loaded correctly.

But if later I move doc1.html to ~/public_html/doc_old/doc1.html, the relative path from doc1.html to pic1.jpg will not hold any longer. I just wonder if I could specify some absolute path for pic1.jpg in doc1.html for external accessing?


There are two different paths, you should not confuse.

On the server the file is located at ~/public_html/something.html, but the webserver uses the ~/public_html/ directory as document root, so the file is published at /something.html.

Now when the browser loads the /something.html path, it cannot access things outside of the document directory, so an absolute path has to consider that. If a path would point at ~/public_html/something-else.html now, the browser would try to find it at /~/public_html/something-else.html or just try to interpret it completely resulting in either an error, or a local access to that file (on linux, where ~ would be a valid path)


An absolute path can be specified by beginning the path with a forward slash (/). This will allow you to refer to documents underneath your document root.

Here is a link to an article on absolute vs. relative paths in HTML.

Edit: in response to your update - the reason you can access pic1.jpg as ../pic/pic1.jpg from doc1.html is that doc1.html lives in /doc/, so moving up one directory and then down into pic will bring you to pic1.jpg. Similarly, since your document root is ~/public_html/, you can access pic1.jpg as /pic/pic1.jpg from any html file. /pic will be interpreted as ~public_html/pic/.


This assumes you're running Apache under Linux (or another Unix variant), with the use of public_html under user directories enabled.

If your user account has /home/user_name as its home directory and /home/user_name/public_html as its top level web directory for the your web files, and the files you're trying to access are named file1.html and file2.html, then you should be able access these files via the web with something similar to:

Absolute URL's:

  • file 1: http://www.example.com/~user_name/file1.html
  • file 2: http://www.example.com/~user_name/file2.html

Relative URL's: - file2 from file1: file2.html - file1 from file2: file1.html

If you're involving a subdirectory, named dir1, with file file3.html,

  • Absolute URL: http://www.example.com/~user_name/dir1/file3.html
  • Relative URL for file3 from file1 or file2: dir1/file3.html
  • Relative URL for file1 from file3: ../file1.html

Of course, within an html file, you'll need to add descriptor text as appropriate.

Edit in response to the updated question:

The absolute URL to the picture file should be:

  • www.xxx.xxx/~tim/pic/pic1.jpg

If - as in your example - you simply move doc1.html from one directory to another at the same level, the relative path to pic1.jpg should still work. If it's not working, my suggestion is to check the permissions of the new directory - does your web server have read permission to the new directory? OTOH, if you move to another level subdirectory (e.g. public_html/doc_old/really_old), you'll have to adjust your relative path to include the subdirectory. For my example, the relative path would become:

  • ../../pic/pic1.jpg
0

精彩评论

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

关注公众号