Opened 17 years ago
Closed 17 years ago
#4554 closed defect (bug) (fixed)
Canonical trailing slashes
Reported by: | ryan | Owned by: | markjaquith |
---|---|---|---|
Milestone: | 2.3 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Administration | Keywords: | |
Focuses: | Cc: |
Description
A post shouldn't be served via both /2007/06/foo and /2007/06/foo/. One should redirect to the other with the user's trailing slash preference as specified in the permalink structure being the canonical form.
Attachments (3)
Change History (21)
#2
@
17 years ago
- Milestone set to 2.3 (trunk)
Oooh, and it also handles truncated URLs. Like if in an e-mail something got changed to:
http://example.com/2007/05/03/title-of- my-awesome-post/
Clicking on http://example.com/2007/05/03/title-of- should forward you.
#4
@
17 years ago
We use that code on wpcom and it breaks plugins that add new links. I have to turn it off for several clients. That's why my patch is more limited. The link guessing when there's a 404 can be added back in. The stuff that builds a link from the query vars can probably be added back if we do it only when ! $wp->did_permalink. It's still pretty iffy though since it doesn't know what query vars have been added via plugin.
#5
@
17 years ago
For example, one client wanted links of the form /2007/week24/. The maps to a query of index.php?year=2007&w=24. The permalink redirector changes this to /2007/ since it doesn't know about "w". Since the link is already a permalink, simply not redirecting it will fix this situation. Mapping a request of index.php?year=2007&w=24 to a permalink will still break, however.
#7
@
17 years ago
Or having a way for plugins to register query vars and such so that it redirects properly, assuming there isn't one already.
#8
@
17 years ago
There is such a way... but the problem is that there are query vars we know about but don't know how to translate into a permalink. Like "w". And it's hard to tell whether a query var is default or was overridden. Big mess for little payoff.
New approach: look for known invalid alternative permalinks like ?p=N and redirect those.
We can still do trailing slash, no-www/yes-www, index.php$ stripping and 404-guessing, which will be the majority of the issues.
#9
@
17 years ago
Instead of just validating that the URL is using the correct format, then it would be nice if it validated that the url was the official permalink:
#11
@
17 years ago
If the there was canonical urls for posts, pages and categories, then it would be a big step. Don't mind this issue being solved in multiple steps, and taking the easy ones first instead of trying to solve everything in one step.
#12
@
17 years ago
- Owner changed from anonymous to markjaquith
- Status changed from new to assigned
canonical_rehash.001.diff
is pretty much a rewrite.
The basic idea here is to look for known issues. It doesn't try to be the end-all. It fixes what it can, and doesn't bite off more than it can chew.
Features:
- Trailing slashes (enforces user preference)
- example.com vs www.example.com (enforces user preference)
- strips trailing /index.php or /index.php/
- strips /index.php/ if using mod_rewrite permalinks, which allows for seamless migration from PATHINFO to mod_rewrite
- Redirects ?p=N, ?page_id=N, ?m=YYYY, ?m=YYYYMM, ?m=YYYYMMDD, ?year=YYYY, ?year=YYYY&monthnum=MM, ?year=YYYY&monthnum=MM&day=DD, ?cat=N, ?author=N, ?paged=N to their "pretty" counterparts.
- Always preserves additional args in the query string.
- Redirects is_single() URLs with partial slugs. e.g. /2007/06/05/slug-fragment => /2007/06/05/slug-fragment-is-whole-now/
- Is non-greedy -- looks for specific cases that it can fix, but doesn't try to fix everything.
I've used dozens of test cases, but I probably forgot a few along the way. Let me know what you think.
#13
@
17 years ago
Since this patch expects that REQUEST_URI is available and correct, then it is even more important that #3514 is solved or else it will redirect constantly on IIS.
canonical.php
is some code Matt and I threw back and forth a few times in March. Handles a whole lot of situations including trailing slashes, yes-www/no-www, and alternative permalink styles (i.e. ?p=N when using pretty permalinks).