How to make href's work as expected - url

I have two questions here, that I thought there were already asked, but I could not find anything related.
Let's suppose I have the following URL:
http://www.domain.com/folder/page
And I have an anchor like this:
Page2
First:
Of course when it is clicked, it will navigate to
http://www.domain.com/folder/page2
But if the user has this URL:
http://www.domain.com/folder/page/ <-- Note the last slash
Then the anchor will navigate to:
http://www.domain.com/folder/page/page2
The first question is:
How can I avoid this?
And the second question would be:
How to always do this?
I mean that even if the url ends with a slash or not, navigate to:
http://www.domain.com/folder/page/page2
I know I can do this with javascript, but the idea is to keep using the href without using javascript in every case this happens. I also know I can use relative urls starting with / to referrer the root, but I can't in this case because the url has some IDs in the middle that may change.

Your basic problem is that you have two URLs that resolve to the same resource.
Pick one of them to be canonical and redirect from the other one two it using HTTP.
Failing that, use root relative URIs:
href="/folder/page2"

Related

Get url of Orchard default pages

I am using orchard 1.9 and I am building a service in which I need to get current URL.
I have OrchardServices and from that I can get the URL like so:
_orchardServices.WorkContext.HttpContext.Request.Url.AbsolutePath;
This works like a charm for pages/routes that I have created but when I go to the Login or register page (/Users/Account/LogOn) the absolute URL is / and I can't find anyway to get the URL or at least any indication that I am in the LogOn or Register.
Anyone knows how I could get the full url?
If I understand what your're asking, you could use the ItemAdminLink from the ContentItemExtension class.
You will need to add references to Orchard.ContentManagement, Orchard.Mvc.Html and Orchard.Utility.Extensions, but then you will have access to the #Html and #Url helpers.
From there you will have the ability to get the link to the item using:
#Html.ItemDisplayLink((ContentItem)Model.ContentItem)
The link to the item with the Url as the title using: #Url.ItemDisplayUrl((ContentItem)Model.ContentItem)
And you should get the same for the admin area by using these:
#Html.ItemAdminLink((ContentItem)Model.ContentItem)
#Url.ItemAdminUrl((ContentItem)Model.ContentItem)
They will give you relative paths, e.g. '/blog/blog-post-1', but it sounds like you've already got a partial solution for that sorted, so it would be a matter of combining the two.
Although I'm sure there are (much) better ways of doing it, you could get the absolute URL using:
String.Format("{0}{1}", WorkContext.CurrentSite.BaseUrl, yourRelativeURL);
...but if anyone has a more elegent way of doing it then post a comment below.
Hope that helps someone.

Why is the hash removed from the URL when you're at the root path?

In the URL, when I want to visit root_path/#hash it removes the "#". Does anyone know why?
For example, if you go to somesite.com/#some_hash, the url gets changed to somesite.com/some_hash, and then when you try to reload the page, it tries to visit the /some_hash path.
I've noticed this only happens with the root path. For example, if you go to somesite.com/somepath/#some_hash, it does not have the same problem. The "#" is preserved.
Why does this happen and is there any way to prevent it from happening? This creates problems when trying to create links to sections on the same page.

What's the difference between beginAt and gotoPage in JWebUnit?

JWebUnit.beginAt:
Begin conversation at a URL absolute or relative to base URL. Use getTestContext().setBaseUrl(String) to define base URL. Absolute URL should start with "http://", "https://" or "www.".
JWebUnit.gotoPage:
Go to the given page like if user has typed the URL manually in the browser. Use getTestContext().setBaseUrl(String) to define base URL. Absolute URL should start with "http://", "https://" or "www.".
So, one says "Begin conversation at URL absolute or relative to base URL", while the other says "Go to the given page like if user has typed the URL manually in the browser". This doesn't help me in the slightest in understanding them (well, specifically the former; the latter makes sense). What's the actual difference between them? Which should I be using, and when?
I finally did manage to find the answer in the source code.
beginAt does two things: start the browser, then call gotoPage with its argument. Thus, you need to use beginAt the first time, and gotoPage subsequent times. (Perhaps if managing multiple windows it has more use; I haven't dug that deeply.)

How do SO URLs self correct themselves if they are mistyped?

If an extra character (like a period, comma or a bracket or even alphabets) gets accidentally added to URL on the stackoverflow.com domain, a 404 error page is not thrown. Instead, URLs self correct themselves & the user is led to the relevant webpage.
For instance, the extra 4 letters I added to the end of a valid SO URL to demonstrate this would be automatically removed when you access the below URL -
https://stackoverflow.com/questions/194812/list-of-freely-available-programming-booksasdf
I guess this has something to do with ASP.NET MVC Routing. How is this feature implemented?
Well, this is quite simple to explain I guess, even without knowing the code behind it:
The text is just candy for search engines and people reading the URL:
This URL will work as well, with the complete text removed!
The only part really important is the question ID that's also embedded in the "path".
This is because EVERYTHING after http://stackoverflow.com/questions/194812 is ignored. It is just there to make the link, if posted somewhere, if more speaking.
Internally the URL is mapped to a handler, e.g., by a rewrite, that transforms into something like: http://stackoverflow.com/questions.php?id=194812 (just an example, don't know the correct internal URL)
This also makes the URL search engine friendly, besides being more readable to humans.

jquery ajax load (full URL)

hi is there a wait to load a full url.?
url= 'http://www.example.com/whatever.php'
$('#selector').load(url); // this way returns null (empty result)
instead of :
url = 'whatever.php'
$('#selector').load(url); // works fine
Some may think whats the difference i want to use this because im using multiple directories. so i could be on a page like...
example.com/dir/
but the dir folder will not have the whatever.php
so anyone has a fix for this that i should always use the full url?
thank you.
You could always use relative paths
putting / before the path will tell the browser to go the root of the page. For your example you could call /whatever.php.
You can also move up one directory at a time. Lets say you are in a page at http://www.example.com/dir/foo/bar.php and want to access something in the dir folder, you could specify ../inTheDir.php to move up one directory or ../../inTheRoot.php to move up two.
This should work for you, but based on your comment it sounds like you have a problem somewhere else since your www. page doesn't seem to respond correctly.
No, there isn't.
If http://www.example.com/ takes longer to load than http://example.com/ then it is probably because you have the DNS record for example.com cached but not the record for www.example.com.
Corrected after having realized a typo changed the meaning of the question.:
This is a case of having a mismatch between the host name the page is loaded from and the host name the Ajaxed resource is requested from. i.e. The Same Origin Policy.
Pick a host name to be canonical, use that one in your requests, and redirect (with a 301 status code) from the other so that people don't go to the wrong one by mistake.

Resources