Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTful cookie path fails in IE without trailing slash

I have been asked to build a tabbed section on pages that have RESTful URLs without a trailing slash (.NET MVC), for example http://myhost/books/the-amber-spyglass

For the tabbed sections to be a bit more user friendly I want them to remember which tab was open on each page as the user moves around, so that if they return to a book they were previously on the same tab remains opened. This is achieved by setting a cookie named "tab" with value set to the tabid and path set to the page they are on (so it doesn't affect tabs on other pages). So far pretty basic stuff you'd think, and it does work quite nicely too.

Enter Internet Explorer.

In IE it turns out a cookie with path /books/the-amber-spyglass will NOT match the above URL and consequently won't get set properly. If I add a trailing slash so the path is /books/the-amber-spyglass/ instead it works fine - but I cannot change our URL schema, and even if I could "the-amber-spyglass" is a resource, NOT a folder.

Does anyone have a recommended solution to this, surely exceedingly common, problem?

Many thanks in advance,

JS

like image 991
John Schulze Avatar asked Jan 28 '10 17:01

John Schulze


People also ask

Is trailing slash required?

The trailing slash does not matter for your root domain or subdomain. Google sees the two as equivalent. But trailing slashes do matter for everything else because Google sees the two versions (one with a trailing slash and one without) as being different URLs.

Should you include trailing slash in URL?

If your site has a directory structure, it's more conventional to use a trailing slash with your directory URLs (for example, example.com/directory/ rather than example.com/directory ), but you can choose whichever you like. Be consistent with the preferred version. Use it in your internal links.

Should APIs end with slash?

Most APIs I've seen don't end with slashes. To some people, ending with a slash (and requiring this) might be surprising behavior. No official sources, because I don't think they exist.

How do I remove trailing slash from URL?

Use the String. replace() method to remove a trailing slash from a string, e.g. str. replace(/\/+$/, '') .


1 Answers

See http://blogs.msdn.com/ieinternals/archive/2009/08/20/WinINET-IE-Cookie-Internals-FAQ.aspx

Due to an obscure bug in the underlying WinINET InternetGetCookie implementation, IE’s document.cookie will not return a cookie if it was set with a path attribute containing a filename.

For instance, if a page sets a cookie on itself like so:

Set-Cookie: HTTPSet-PathCookie=PASS;path=/check.htm

…the cookie will be sent with HTTP requests but will not appear in the document.cookie collection.

like image 71
EricLaw Avatar answered Oct 24 '22 04:10

EricLaw