[wp-trac] [WordPress Trac] #51725: Canonical redirect and user_trailingslashit()
WordPress Trac
noreply at wordpress.org
Sat Nov 7 01:06:54 UTC 2020
#51725: Canonical redirect and user_trailingslashit()
-------------------------------------------+-----------------------------
Reporter: mbis | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Permalinks | Version: 5.5.2
Severity: trivial | Keywords:
Focuses: performance, coding-standards |
-------------------------------------------+-----------------------------
Hi,
this bug is quite difficult to spot and it appears only when
''redirect_canonical()'' function is used to force the canonical redirect
on front page and the trailing slashes are not added to the permalinks.
Happily it is nothing serious.
**How to reproduce this bug?**
**Step 1. Remove the trailing slashes from WP permalinks:**
First of all, you need to use "Custom structure" mode in Permalinks
settings and remove the trailing slash (''/%postname%/'' =>
''/%postname%'') in the adjacent input field. Therefore, the value of
''$wp_rewrite->use_trailing_slashes'' is changed to false.
**Step 2. Trigger the canonical redirect on front page**
Now let's disable the feed URL by redirecting the visitors trying to
access it:
{{{#!php
<?php
function disable_feed() {
if(is_feed() && is_front_page()) {
redirect_canonical();
}
}
add_action('template_redirect', 'disable_feed', 1);
}}}
Now, when one tries to access the feed URL linked to front page, (eg.
http://example.com/feed), the above hook will be triggered and visitor
redirected to the canonical URL (''http://example.com/feed'' =>
''http://example.com'').
The problem is that the PHP notice is displayed then:
''<b>Notice</b>: Undefined index: path in
<b>/var/www/vhosts/maciejbis.net/example.com/wp-includes/canonical.php</b>
on line <b>576</b><br />''
I guess that the source of the problem is here (line 501):
https://core.trac.wordpress.org/browser/tags/5.5.1/src/wp-
includes/canonical.php#L501
{{{#!php
$redirect['path'] = user_trailingslashit( $redirect['path'] );
}}}
Unfortunately because of ''user_trailingslashit()'' function, the slash is
removed and value is set to ''null''. The value of `$redirect['path']`
should not be changed in this particular case (value '/' should be
preserved).
My first suggestion is to exclude ''user_trailingslashit()'' function
there and not use it when value of `$redirect['path']` variable is just a
single slash '/':
{{{#!php
$redirect['path'] = ( $redirect['path'] === '/' ) ? $redirect['path'] :
user_trailingslashit( $redirect['path'] );
}}}
Another idea is to do the same but directly in ''user_trailingslashit()''
function:
https://core.trac.wordpress.org/browser/tags/5.5.1/src/wp-includes/link-
template.php#L51
Basically this piece of code:
{{{#!php
if ( $wp_rewrite->use_trailing_slashes ) {
$string = trailingslashit( $string );
} else {
$string = untrailingslashit( $string );
}
}}}
can be replaced with:
{{{#!php
if ( $wp_rewrite->use_trailing_slashes ) {
$string = trailingslashit( $string );
} else if ( $string !== '/' ) {
$string = untrailingslashit( $string );
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/51725>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list