How does the browser resolve the relative location of './'? - html

I am combining many small semi-static, single-page webapps into one larger web site. The backend is a lot of proxies, but the forward facing server basically just make it look like the app was moved from the root filepath to a more specifics one. IE:
/
├── css
│   └── app1.css
├── index.html
└── js
└── app1.js
would be moved to
/apps/app1/
├── css
│   └── app1.css
├── index.html
└── js
└── app1.js
This migration has been relatively painless mainly due to the use of ./ in the apps' html files, such that most apps just load their resources relative to their new location. The problem I am having is that some apps are resolving ./ differently. For these trouble cases, the primary html file gets loaded; however, the ./ in the script and style elements are resolving to a higher file-path (IE: I would expect ./ to resolve to /apps/app1 but am getting /apps). It may be a coincidence, but the troubled apps often have additional, non-index HTML files.
What are the rules for how ./ is resolved?

Determine the base URL
This is usually the URL of the HTML document
It might be overridden by the base element
For CSS it is the URL of the stylesheet
JS is always with respect to the HTML document
Remove everything after the last / in the path section of the URL
e.g. the base URL for https://example.com/example/foo?bar=baz#fragment is https://example.com/example/
Keep in mind that an HTML document might be visible at the path /example and /example/ and you should avoid this by making one path canonical (I prefer the one that ends in a /) and redirecting to it from the other
Strip the ./ from the front of the relative path
Append the result of step 3 to the result of step 2
A common gotcha is to confuse URLs with file paths. While a simple static site will usually have a direct 1:1 mapping between them, many modern sites will use routing code (e.g. for Express for HTML documents and a separate static route for static files like images, js and css.

Related

How to insert an image in my post on Hugo?

Here is my repository on my Hugo blog:
I'd like to insert an image to a post with the following text:
![Scenario 1: Across columns](content/post/image/across_column.png)
However, it does not come out and it gives an error of 404 - Page not found.
What am I making wrong here?
You have a typo in the image link. You have an images directory, but reference "content/post/image/..." without the "s". That won't fix it for you though.
There are a few ways to link images.
Option 1. Put all of your images in the static/ directory. Then reference the image file with a leading slash, e.g.:
![Scenario 1: Across columns](/across_column.png)
Option 2. Use sub-directories to hold the markdown file and any related resources.
create a directory post/creating-a-new-theme
move your existing markdown file into that directory, and rename it to index.md
create a subdirectory post/creating-a-new-theme/images and move your images in there
reference the image as ![Image alt](images/my-image.jpg)
More info on option 2: https://github.com/gohugoio/hugo/issues/1240#issuecomment-753077529
More options
There are more sophisticated ways to reference images using the frontmatter, as well: https://gohugo.io/content-management/page-resources/
TL;DR
Put your images in the static directory, just like below, use it in markdown like ![targets](/images/my_post_folder/my_image.png) or ![targets](/images/my_image2.jpg) if you don't want to build a post folder
If you search the hugo documentation, you can find Image Processing | Hugo
But! That's no a markdown way to insert an image. If you don't miss the Getting Stated, you will find the static Directory, which says can store images, that's it!
[static](https://gohugo.io/content-management/static-files/)
Stores all the static content: **images**, CSS, JavaScript, etc. When Hugo builds your site, all assets inside your static directory are copied over as-is. A good example of using the static folder is for verifying site ownership on Google Search Console, where you want Hugo to copy over a complete HTML file without modifying its content.
How to use it
Put your images in the static directory, just like below, use it in markdown like ![targets](/images/my_post_folder/my_image.png) or ![targets](/images/my_image2.jpg) if you don't want to build a post folder
static
└── images
├── my_post_folder
│   ├── my_image.png
└── my_image2.jpg

I cannot deploy static site to Netlify

I am trying to deploy some ui components on Netlify. However, it is not recognizing my index.html files inside the subfolders. Hence nothing is showing up on my deployed site. Also, all 3 index files have links to each other. This is my file structure
├── ui-components
├── blog-cards
│ └── index.html
├── login
│ └── index.html
├── ads-manager
│ └── index.html
Do I have to delete my subfolders and bring out my index files for it to deploy on Netlify or is there any way around it?
Edit:
I made some progress by doing this instead by putting a _redirects file in the root of my app as suggested
/ /blog-cards/index.html 200
/login /login/index.html 200
/ads-manager /ads-manager/index.html 200
It's finding my index.html inside the blog-cards folder however it's not loading my css file now. Full folder structure
├── ui-components
├── blog-cards
│ └── index.html
│ └── style.css
│ └── images
Here's a link to the netlify site
You don't have a main index.html file, which is why nothing is showing up on your deployed site. You should add something there, even just a blank page or a placeholder with links to the other projects.
You probably don't have to though, just browse to <siteurl>/ui-components/blog-cards and that should work.
Update after further comments.
If there is a subfolder you want the site to automatically go to upon landing, then you can use Redirects.
For example, putting the below in a file called _redirects in the root of your app will do what I think you need:
/ /ui-components/blog-cards 200
/login /ui-components/login 200
/ads /ui-components/ads-manager 200
(the 200 status code means that it will be a redirect under-the-hood, which makes the URL stay clean as you have said.)
Update
Yeah, so the forwarding is making the .css files to fail to load.
<link rel="stylesheet" href="style.css"> will look at the current URL and add style.css to it, which is obviously not what you want.
You could change to forwarding it with a 302, which would mean the new (not nice) URL would show up, but then the css path would resolve correctly.
Alternatively, you could add a second stylesheet link that looks for ./blog-cards/style.css.
Or if the css is small enough you could inline it, making it all simpler.

Displaying a short URL for a website

I am not clear on how to frame my question. I am making the website for my college fest, and although I know HTML, CSS, I don't know anything of PHP. I just want to display my URLs like this:-
www.somesite.com/somepage (something like that)
instead of :-
www.somesite.com/somepage/index.html
How do I do that? Thanks for any help. I am new to all this
edit: I have tried something like this, just removed the constraint-
<script>
setTimeout (function () {
if (typeof history.pushState === "function") {
var width = window.innerWidth || screen.width;
if (width < 768) {
history.pushState(null, null, "/short-url");
}
}
}, 10 );
</script>
Didn't work. The console gave an error "Failed to load resource" for a lot of files.
What you are looking for is called URL Rewriting. If you have an access to the .htaccess file
you can define some rewrite rules in it, for example:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^www.somesite.com/somepage/?$ www.somesite.com/somepage/index.html [NC,L]
The first part is the address the user will enter and the second part is the actual address he will be referring to, the third part is an array that contains some flags regarding the rewriting.
NC - the rule should be case-insensitive
L - don't process any more rules if this one is used
Note that you don't have to use the full URL, for example:
RewriteRule ^blog/first-post/?$ /blog/posts/myFirstPost.html [NC,L]
Here's a nice beginners guide.
The answer depends on a particular web server configuration. Usually request url corresponds to a particular directory path relative to site root directory. Some web servers allow to omit such resource names as index.hml, index.php in request url. They will associate implicitly /some/path with /some/path/index.html.
See link
You will want to have a directory structure similar to this:
website/
├── blog
│   └── index.php
├── contact
│   └── index.php
├── index.php
├── projects
│   └── index.php
└── resume
└── index.php
where within each folder (these being the different sections of your website), you have the .php or .html files being named index.extension. This will give you the desired effect.
A really easy and silly way would be to encompass the url you want in an anchor tag and have the anchor tag point to the url you want. for example:
www.somewebsite.com/webpage
Also, depending on the server's OS (I know this is true for apache because it's what I learned on), you don't need to explicitly indicate index.html, it'll load the index of the directory automatically. In some cases, if you exclude the trailing / as you did with www.somesite.com/somepage then it'll redirect to www.somesite.com/somepage/. This happened with my school's website and I don't think they've fixed it.

Using absolute paths in django templates

Recently I've been working on a project in Django that uses a site-wide CSS layout, so I decided that each template (in this case a template in /projects/index.html) used would extend a base file containing the header, footer, javascript, etc. called base.html.
The problem is that my directory structure looks like so:
.
├── static
│   └── base.html
├── templates
│   └── projects
│ └── index.html
And, as you can see the base file I want to extend is in a higher directory than that of the index.html file. Normally, I would use a relative path and use the following code at the top of the index file: {% extends "../base.html" %} or simply use an absolute path to the file (if necessary)
It seems, however, that by using either of these methods, whatever is inside the quotes for extends simply gets appended onto the current path, and my call to the upper directory with .. gets ignored entirely.
That is, if the current path is, for example, /project/templates/projects and I use {% extends "/project/static/base.html" %}, that will be appended to the current path, causing the system to look for /project/templates/projects/project/static/base.html, which, of course, doesn't exist. After researching I came across an article that said the blocking of relative paths is intentional for security purposes, but it leaves me with no way to access any file outside the current working directory.
I figured this had to be an extremely common setup when building a website, and so there must be some sort of way to interact with multiple templates that I'm just not aware of yet. If anyone has any information on that, it would be much appreciated.
Your base.html should be residing in a templates directory and not in the static directory.
This is because you define where django searches for templates using the TEMPLATE_DIRS in your settings.py file. Here, I give an example which computes your TEMPLATE_DIRS value dynamically.
import os
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), '..', 'templates'),
)
In additional, you need to be aware that django depends on another setting called TEMPLATE_LOADERS to determine the priority in which it loads up your html files/templates, while searching through your templates directories.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
Once your base.html is located in your templates directory, all you need to do in your html files is to write:-
{% extends "base.html" %}
and you would extend correctly
All of your html templates should live under the templates directory (including your base.html). The location of this folder is set using the TEMPLATE_DIRECTORY settings in your settings.py. The static folder is solely for css, js, etc.
When inheriting from another template using the extends tag, the path you give is always relative to your template directory, not project.

Having links relative to root?

Is there a way to have all links on a page be relative to the root directory?
For example, on www.example.com/fruits/apples/apple.html I could have a link saying:
Back to Fruits List
Would this link be pointing to www.example.com/fruits/apples/fruits/index.html or www.example.com/fruits/index.html? If the first, is there a way to have it point to the 2nd instead?
A root-relative URL starts with a / character, to look something like link text.
The link you posted: Back to Fruits List is linking to an html file located in a directory named fruits, the directory being in the same directory as the html page in which this link appears.
To make it a root-relative URL, change it to:
Back to Fruits List
Edited in response to question, in comments, from OP:
So doing / will make it relative to www.example.com, is there a way to specify what the root is, e.g what if i want the root to be www.example.com/fruits in www.example.com/fruits/apples/apple.html?
Yes, prefacing the URL, in the href or src attributes, with a / will make the path relative to the root directory. For example, given the html page at www.example.com/fruits/apples.html, the a of href="/vegetables/carrots.html" will link to the page www.example.com/vegetables/carrots.html.
The base tag element allows you to specify the base-uri for that page (though the base tag would have to be added to every page in which it was necessary for to use a specific base, for this I'll simply cite the W3's example:
For example, given the following BASE declaration and A declaration:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>Our Products</TITLE>
<BASE href="http://www.aviary.com/products/intro.html">
</HEAD>
<BODY>
<P>Have you seen our Bird Cages?
</BODY>
</HTML>
the relative URI "../cages/birds.gif" would resolve to:
http://www.aviary.com/cages/birds.gif
Example quoted from: http://www.w3.org/TR/html401/struct/links.html#h-12.4.
Suggested reading:
http://www.motive.co.nz/glossary/linking.php
http://www.communitymx.com/content/article.cfm?cid=AEDCC52C4AD230AD
Use
Back to Fruits List
or
Back to Fruits List
If you are creating the URL from the server side of an ASP.NET application, and deploying your website to a virtual directory (e.g. app2) in your website i.e.
http://www.yourwebsite.com/app2/
then just insert
<base href="~/" />
just after the title tag.
so whenever you use root relative e.g.
<a href="/Accounts/Login"/>
would resolve to "http://www.yourwebsite.com/app2/Accounts/Login"
This way you can always point to your files relatively-absolutely ;)
To me this is the most flexible solution.
Back to Fruits List
Relative Path Summary (applicable to href, src etc.,):
/file_Or_FolderName Root directory
./file_Or_FolderName Current directory
../file_Or_FolderName Previous directory (One level up)
../../file_Or_FolderName Previous of previous directory (Two levels up)
../../../file_Or_FolderName Just like above - Three levels up
Example:
www.example.com
├── apple.html
└── FolderA
├── fileA.html
└── FolderB
├── fileB.html
└── FolderC
├── fileC.html
└── FolderD <------ Suppose you're here (current directory)
├── fileD.html
└── FolderE
└── fileE.html
Following shows how to access the file at different levels using the relative path (applicable to href, src etc.,)
fileD.html - same level access(or)
./fileD.html - same level
./FolderE/fileE.html - 1 level Down
../fileC.html - 1 level Up
../../fileB.html - 2 levels Up
../../../fileA.html - 3 levels Up
../../../../apple.html - 4 levels Up (or)
/apple.html - 4 levels Up but direcly using root /
To give a URL to an image tag which locates images/ directory in the root like
`logo.png`
you should give src URL starting with / as follows:
<img src="/images/logo.png"/>
This code works in any directories without any troubles even if you are in branches/europe/about.php still the logo can be seen right there.
Use this code "./" as root on the server as it works for me
Back to Fruits List
but when you are on a local machine use the following code "../" as the root relative path
Back to Fruits List