Relative Urls and Trailing Slashes

Relative URLs and trailing slashes

No.

The problem is not so much related to the director/file mapping (which has never been expected as how mappings would happen, only allowed as a convenient mapping, which still often are convenient).

It's more related to the simple fact that dolor is not the same as dolor/ and you want to give a new URI from a reference relative to dolor/ when combined to one ending in dolor.

What may be the solution, is to act with /lorem/ipsum/dolor/ all the time. That is to say, never talking about /lorem/ipsum/dolor at all, only ever about /lorem/ipsum/dolor/. After all, since the directory/file mapping is, as you say, not the only way to do things, there's no reason why your resource names shouldn't always end in slashes.

Indeed, this may make more sense anyway, as in using such relative links you are implying that there is some sort of relationship between /lorem/ipsum/dolor/not-dolor and /lorem/ipsum/dolor. Now, while /lorem/ipsum/dolor/not-dolor may not have much of a relationship to /lorem/ipsum/dolor/, the implication that it might is there in the URI (yes URIs are opaque, but also while they must be treated as opaque at some levels, they are allowed to reflect relationships, and this is precisely why relative URI references make sense). Arguably therefore, /lorem/ipsum/dolor/ more clearly reflects your overall URI-to-resource mapping (if it didn't, you wouldn't want to be going from dolor to not-dolor anyway).

Now, this comes down to redirecting to a trailing slash, which you say you want to avoid (or better yet, never leading someone to dolor in the first place), but its advantages now seem better than just the convenience of easier relative URIs.

When should I use a trailing slash in my URL?

In my personal opinion trailing slashes are misused.

Basically the URL format came from the same UNIX format of files and folders, later on, on DOS systems, and finally, adapted for the web.

A typical URL for this book on a Unix-like operating system would be a file path such as file:///home/username/RomeoAndJuliet.pdf, identifying the electronic book saved in a file on a local hard disk.

Source: Wikipedia: Uniform Resource Identifier

Another good source to read: Wikipedia: URI Scheme

According to RFC 1738, which defined URLs in 1994, when resources contain references to other resources, they can use relative links to define the location of the second resource as if to say, "in the same place as this one except with the following relative path". It went on to say that such relative URLs are dependent on the original URL containing a hierarchical structure against which the relative link is based, and that the ftp, http,
and file URL schemes are examples of some that can be considered hierarchical, with the components of the hierarchy being separated by "/".

Source: Wikipedia Uniform Resource Locator (URL)

Also:

That is the question we hear often. Onward to the answers! Historically, it’s common for URLs with a trailing slash to indicate a directory, and those without a trailing slash to
denote a file:

http://example.com/foo/ (with trailing slash, conventionally a directory)

http://example.com/foo (without trailing slash, conventionally a file)

Source: Google WebMaster Central Blog - To slash or not to slash

Finally:

  1. A slash at the end of the URL makes the address look "pretty".

  2. A URL without a slash at the end and without an extension looks somewhat "weird".

  3. You will never name your CSS file (for example) http://www.sample.com/stylesheet/ would you?

BUT I'm being a proponent of web best practices regardless of the environment.
It can be wonky and unclear, just as you said about the URL with no ext.

Built relative URL in pages that are missing trailing slash /

While you could just use relative URLs in your links (with href="./page"), it sounds like the problem is that you are using duplicate IDs (which results in invalid markup). You can test that you have valid markup with the W3C Markup Validation Service.

When you have duplicate IDs, JavaScript only applies to the first element. This can be seen in the following:

var x = window.location.href; // Current page URLvar link = document.getElementById("relurl"); // store the elementvar curHref = link.getAttribute('href'); // Get HREF paramterlink.setAttribute('href', x + "/" + curHref);
<a href="home" id="relurl" target="_blank" title="This is a relative link!">Working Link</a><br /><a href="home" id="relurl" target="_blank" title="This is a relative link!">NOT Working</a>

Client-side relative addressing and allowing no trailing slash in application URL

You need to set the base url in your main template (_Layout.cshtml in ASP.NET Core MVC) to the application path.

In _Layout.cshtml add

<base href="~/"/>

~/ is your applications path (also called web root) that ASP.NET Core will replace with http://example.com/Application. Generally if you need application relative paths you should always do <a href="~/app/relative/path"></a>.

That may proof a bit difficult when static json files are involved, so <base href="..."/> should work in this cases

nginx redirect to trailing slashes and substitude to index.php (if files and folders don't exists)

I found working solution. It uses bad practice (if is evil), but it's only working code. So if any will show better workig decision, I will be grateful.

set $my_var 0;
if (-f $request_filename) {
set $my_var 1;
}
if (-d $request_filename) {
set $my_var 1;
}
if (-d $request_filename) {
set $my_var 1;
}
if ($request_uri ~ "^.*/market/cart$") {
set $my_var 1;
}
if ($request_uri ~ "^.*/market/order/accept$") {
set $my_var 1;
}
if ($request_uri ~ "^.*/market/order/status$") {
set $my_var 1;
}
if ($request_uri ~* "(.+)\.(?:\d+)\.(js|css|png|jpg|jpeg|gif)$") {
set $my_var 1;
}

if ($my_var = 0) {
rewrite ^(.*[^/])$ $1/ permanent;
}

if ($request_uri ~* "^(.*/)index\.php$") {
return 301 $1;
}

location ~* (.+)\.(?:\d+)\.(js|css|png|jpg|jpeg|gif)$ {
try_files $uri $1.$2;
}

location / {
try_files $uri $uri/ /index.php$is_args$args;
}

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-newstom-fpm.sock;
fastcgi_read_timeout 300;
}

Relative URL slash when using base tag

Not really.

Browsers do not simply concatenate your base href attributes and relative URLs.
Instead, <base> tag sets the Document base URL which is then used to dynamically generate a new path.

You can see an interesting example in the WHATWG HTML specification:

<base href="http://www.example.com/news/index.html">
<p>Visit the <a href="archives.html">archives</a>.</p>

Note that the href attribute even ends with a filename.

This means that if you do not put a trailing slash to your base href attribute, foo will be treated as the last, volatile part of the path and will essentially be removed. By way of example:

<base href="http://www.example.com/news/index.html" />
<img src="ball.png" /> <!-- GET http://www.example.com/news/ball.png -->

<base href="http://www.example.com/news/" />
<img src="ball.png" /> <!-- GET http://www.example.com/news/ball.png -->

<base href="http://www.example.com/news" />
<img src="ball.png" /> <!-- GET http://www.example.com/ball.png -->

<base href="http://www.example.com/" />
<img src="ball.png" /> <!-- GET http://www.example.com/ball.png -->

<!-- If served from http://www.example.com: -->
<base href="/" />
<img src="ball.png" /> <!-- GET http://www.example.com/ball.png -->


Related Topics



Leave a reply



Submit