Ticket #2 (defect)
Opened 4 years ago
Last modified 4 years ago
Auto-redirect to canonical URLs
Status: closed (wontfix)
| Reported by: | hmans | Assigned to: | hmans |
|---|---|---|---|
| Priority: | normal | Milestone: | 2.0-alpha |
| Component: | CherryPy code | Keywords: | |
| Cc: | cherrypy@attardi.org |
The canonical URL scheme looks like this:
- / -> cpg.root.index()
- /index -> cpg.root.index()
- /a/ -> cpg.root.a.index()
- /a/b -> cpg.root.a.b()
In other words, objects always have trailing slashes, methods never have trailing slashes.
CP2 should enforce this scheme by automatically doing an external redirect if the incoming request URI isn't "correct". For example, if the user requests /a, CP2 should redirect to /a/ before actually calling cpg.root.a.index().
If we do this, request handler classes can safely use relative HTML links to refer to their own (or their parent/child objects') methods.
See: http://sourceforge.net/mailarchive/message.php?msg_id=9558209
Change History
09/20/04 08:11:15: Modified by hmans
09/25/04 21:00:31: Modified by sja
See #4 for a patch that fixes this.
09/25/04 21:02:15: Modified by sja
- cc set to cherrypy@attardi.org.
09/26/04 09:29:23: Modified by sja
Should URIs with virtual path parameters have a trailing slash?
My patch strips trailing slashes from such URLs, because I thought the most common case would be someting like /user/12, where "12" logically represents a file, or a leaf in the tree.
But what should happen when we have urls such as /articles/2004/, which returns a list of articles from the given year? Is this an index/folder? Should it have a trailing slash? If so, how do we distinguish between the former and the latter case?
Perhaps we could add a trailing slash when the list of parameters is incomplete. For example:
class Articles: def default(self, year, month=None, day=None, number=None): ...
Given the previous method definition, these are the possible valid urls:
/articles/2004/ /articles/2004/12/ /articles/2004/12/5/ /articles/2004/12/5/2
09/26/04 10:05:10: Modified by sja
I've posted an updated patch which implements the behavior I've just described in #4.
09/26/04 10:26:56: Modified by sja
There are a couple open issues with my latest implementation.
class A: def b(self, c=None): ...
In this case, b is a method, hence it will not have a trailing slash in the url (/a/b). But b allows a virtual path parameter (/a/b/45), so it's logically not a leaf, but an index... should it have a trailing slash when the parameter is unspecified?
class Articles: # colorScheme is meant as a query string parameter def default(self, year, month=None, day=None, number=None, colorScheme=1): ...
If a method is defined as in this example, the URL of an article will have a trailing slash (/2004/12/5/3/), though it problably should not.
A workaround is to define the method thus:
def default(self, year, month=None, day=None, number=None, **parameters): ...
Personally I like this better because it creates a cleaner distinction between normal parameters and query string arguments.
11/02/04 10:05:59: Modified by anonymous
- status changed from new to closed.
- resolution set to wontfix.


After some discussion on cherrypy-devel, we're considering making this a configuration option.