I'm sorry if this has been asked but I can't find it and am not sure what I'm even searching for.
I have links in my web app (Hosted on google app engine) on a page at /search/ like:
<a href="./details/">Details</a>
or
<a href="details/">Details</a>
both work as long as /search/ has the trailing backslash (pointing at /search/details/), but break if /search is missing the trailing backslash (instead pointing at /details/)
putting the link as:
<a href="/search/details/">Details</a>
always points towards the right place, but this won't work as I have one template for search results and they sometimes appear on pages other than /search/ and each one needs their own unique details request handler.
Is there a way to make sure that the
<a href="./details/">Details</a>
syntax doesn't break??
Thanks to help from deceze, I came up with the solution which is to always enforce trailing slashes on my urls and stick with the relative structure. I redirect all requests without the trailing slash with:
('.*[^/]$', AddSlashHandler),
and the handler does this:
class AddSlashHandler(Handler):
def get(self):
if self.request.path:
red_url = self.request.path + '/'
if self.request.query_string:
red_url += '?' + self.request.query_string
self.redirect(red_url)
else:
self.redirect('/')
You only have two options:
An absolute path starts with /
and is always absolute to the root of the page; meaning /foo/bar
always resolves to http://www.example.com/foo/bar
.
Relative paths do not start with /
and their rules are a bit more complicated. Essentially, relative paths are appended to the last /
in the current URL. If you're on /foo
, the relative URL bar
resolves to /bar
. If you're on /foo/
, it resolves to /foo/bar
instead.
It is up to you to handle this correctly. Typically in a server-side framework (PHP or such), you have some function which can correctly generate an absolute URL to the desired location. Investigate URL routers for this purpose. Since you're saying you are using the same page on different URLs, I suspect you're reusing the same template within different contexts, which implies some sort of server-side component, so establishing a sane URL handling strategy there should be your priority.