Absolute vs relative URLs

后端 未结 12 1413
时光取名叫无心
时光取名叫无心 2020-11-21 05:31

I would like to know the differences between these two types of URLs: relative URLs (for pictures, CSS files, JS files, etc.) and absolute URLs.

In addition, which o

12条回答
  •  野性不改
    2020-11-21 05:53

    Let's say you have a site www.yourserver.com. In the root directory for web documents you have an images sub-directoy and in that you have myimage.jpg.

    An absolute URL defines the exact location of the document, for example:

    http://www.yourserver.com/images/myimage.jpg
    

    A relative URL defines the location relative to the current directory, for example, given you are in the root web directory your image is in:

    images/myimage.jpg
    

    (relative to that root directory)

    You should always use relative URLS where possible. If you move the site to www.anotherserver.com you would have to update all the absolute URLs that were pointing at www.yourserver.com, relative ones will just keep working as is.

提交回复
热议问题