HTML form action relative path wrong - html

I have a form with a relative path action action="updateItem/:id" in the route /wishlist/admin. I expected this to post to /wishlist/admin/updateItem/:id but it's posting to /wishlist/updateItem/:id instead.
Is there any way to make this relative to the current page or should I add admin/ to the start of the form action?

A relative path replaces everything after the last / in the existing path.
Your base URL needs to be /wishlist/admin/ and not /wishlist/admin.
I generally recommend using absolute paths to avoid any issues with having two similar URLs like that.

Related

Why does href="/../subpage/doc.html" work?

The website I found looks like the following:
current URL: http://www.example.com/stocking/
a link: <a href="/../shop/alphabetic/page">
this takes you to http://www.example.com/shop/alphabetic/page.
From what I understand about relative paths, you use a leading slash to refer to the current base URL and leading points to go from the current directory. Therefore, it should make no sense to do the above.
Actually, I'm surprised this is even working and somehow equivalent to either
href="../shop/alphabetic/page"
href="/shop/alphabetic/page"
which should work as well for this purpose.
So how does this even work?
/ starts an absolute path.
../ then goes up a path segment, but as you are at the top already, it has no effect and is ignored.

How to go to sub-directory with relative URI

Sorry if it so basic but I could not find the answer by searching.
If we are in the page http://www.example.com/a-dir-without-trailing-slash how we can reach the sub-directory http://www.example.com/a-dir-without-trailing-slash/pic using relative URI? (we do not know the current directory name(i.e. a-dir-without-trailing-slash)
Some more explanation:
a-dir-without-trailing-slash is the name of an article in the website. It is not an actual directory nor an actual file name. Now, I want to get the pictures that are used in this article by addresses like:
http://www.example.com/a-dir-without-trailing-slash/pic/1
http://www.example.com/a-dir-without-trailing-slash/pic/2
,...
and in the webpage html, I would refer to them with something similar to:
<img src="pic/1" />
If the original article address was in the form of http://www.example.com/a-dir-with-trailing-slash/, the above example would work finely. I want to know if is it possible to get a relative URI with current article addresses (without trailing slash)?
Thank you very much
I suppose you want to avoid hard coding "slugs" in the content so that they can be stored and manipulated independent of each other.
One solution is to use the base tag which allows you to specify the prefix that is added to relative URLs instead of typing them all over the place.
Make sure that your website uses absolute URLs where necessary.
Modify your CMS to "generate" and place the following tag inside the head section that contains trailing slash:
<base href="/a-dir-without-trailing-slash/">
Then you can use relative URLs inside the content, for example:
<img src="pic/1">
<!-- http://www.example.com/a-dir-without-trailing-slash.html/pic/1 -->
You need server side scripting to add the filename to urls (or may be just one '> tag in the head). – Salman
Bounty get.

Why Does this relative URL work?

So I've been playing with writing some web crawlers and testing them on different sites. But I've come across some sites that seem like their relative urls should not work, or at least I think they should point to someplace other than where the browser resolves them to.
Given a url of a current page : "http://www.examplesite.com/a/page.htm"
And a link of: "a/page2.htm"
The browser correctly resolves this as: "http://www.examplesite.com/a/page2.htm"
My problem/feeling (obviously wrong, but I'm wondering why) is that this should resolve to "http://www.examplesite.com/a/a/page2.htm". The relative url does not begin with a /, so why does it become base relative?
Interestingly, Java's URL class appears to agree with me, as the following code will output : "http://www.examplesite.com/a/a/page2.htm"
URL baseUrl = new URL("http://www.examplesite.com/a/page.htm");
URL absoluteURL = new URL(baseURL,"a/page2.htm");
Why does this link resolve the way it does, and what is the formal rule for resolving a relative link like this?
EDIT:
I just notice that in the <head> portion of the webpage there is a field like so:
<base href="http://examplesite.com/">
I'm assuming that this overrides any relative links to use that as its base url instead of the actual url. Is this a correct assumption? Is that even a valid html markup?
You are correct in that it is the base tag, and yes it is valid.
In HTML, links and references to external images, applets,
form-processing programs, style sheets, etc. are always specified by a
URI. Relative URIs are resolved according to a base URI, which may
come from a variety of sources. The BASE element allows authors to
specify a document's base URI explicitly.
When present, the BASE element must appear in the HEAD section of an
HTML document, before any element that refers to an external source.
The path information specified by the BASE element only affects URIs
in the document where the element appears.
Sources: W3C Wiki and W3C Markup
The site is likely using a <base> tag to specify the parent as the prefix to all relative URL's on the site.
You can find out more on the base tag here. If this is not the case, then please provide the source URL as this defies normal behavior.

Absolutizing an image url with a ".."

I have an HTML document I'm transforming with an image whose source url looks like this:
"../foo/bar/baz.png"
I'm using a tritium function to absolutize image source urls, but the ".." seems to be stumping it. It's prepending the hostname, etc, but when it does, it adds one too many layers.
So for example, the correct URL of the image is:
"www.host.com/foo/bar.png"
But the page on which it appears is at "www.host.com/site/baz/page.html"
The source of the image in the original html is therefore "../foo/bar.png"
But the absolutized result I'm getting is: "www.host.com/site/foo/bar.png"
In other words it's going up the file tree to "/site/", but it needs to be going up one more. I don't really see how it even works on the original page without another ".." How should I be handling the ".." in the url?
.. means to traverse one level up; you are using a relative path, not an absolute one like you should be. Drop the dots:
<img src="/foo/bar.png"> will load the image from the root of the domain.
There is a huge difference between src="/foo/bar.png" and src="foo/bar.png" (Notice the slash after the first double quote)
First one points to http://example.com/foo/bar.png NO MATTER what.
Second one, however, (without the beginning slash) is relative URL so the output path depends on the file on which the image appears.
That is why you were getting "www.host.com/site/foo/bar.png" (one level up relative to the file path).
Two solutions:
1) src="/foo/bar.png" OR
2) src="../../foo/bar.png"
I always recommend the first approach because even after you move the files around, you won't have to change the absolute URL. (I learned it the hard way)
P.S. this rule applies to CSS files as well. (for example when specifying the background image URL) If you use absolute paths, you won't have to bang your head on the wall when you change the directory of the CSS file.
As you're in a Moovweb project, I would suggest manipulating the problematic src before you use the absolutize() function.
Is there an easy way you can select the image using Tritium? I'd suggest doing that, then manipulating the src attribute:
$("./img[#id='']") {
attribute("src", "/foo/bar.png")
}
After this, you should be able to use the absolutize() function and the src will be rendered correctly.

Alternatives to base tag

I used mode rewrite on my website
I used the base tag to solve my relative links problem
<base href="/" />
But the problem is absolute link eg. http://www.absolutelinks.com
It changes it to www.mysite.com/http://www.absolutelinks.com
How can i fix this
Base href applies only to the relative URL so if you have got: Google you'll be redirected to Google, not http://mydomain/http://google.com/. Please post the code of your HTML document.
However using base isn't the best practice. Much better approach is to use absolute URLs like: src="/styles/main.css" which always points to mydomain/styles/main.css.
Don't use <base> at all, instead have some server-side config and keep a $base variable there - then, when outputting any URL during your HTML generation use {$base}{$restofurl}.
This works well when you have the same code running in development/test/live environments - you just need to change your server-side $base config.
Using PHP/Smarty syntax above but I'm sure you get the idea.