Make WordPress Core

Opened 15 months ago

Last modified 15 months ago

#51700 new defect (bug)

Infinite redirect loop error when blog site is rewritten and real domain on another server

Reported by: vladop Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.5.2
Component: Canonical Keywords:
Focuses: Cc:


I have my main site at: http://mainsite.com hosted on server A.
I have my wordpress blog hosted on some other domain however it is rewritten to http://mainsite/blog (although under the hood is is on another domain http://myblogonmyserver.com) and it is hosted od server B.
Both machines are Windows Server machines and PHP is 7.4 altought version of php is not an issue here. This bug is there for at least year. I found solution and source of bug.

It is inside canonical.php file (around line 65)

In latest version of wordpress (5.5.3) it is in fact around line 71 . I just added this part of code:

$sURL = site_url();
        $asParts = parse_url( $sURL );
$requested_url .= $asParts['host'] ;

after this if statement:

if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) {
                // Build the URL in the address bar.
                $requested_url  = is_ssl() ? 'https://' : 'http://';
                $requested_url .= $_SERVER['HTTP_HOST'];
                $requested_url .= $_SERVER['REQUEST_URI'];

and now works in my case. Issue is that I have update  this part of code every time I update wordpress

Change History (3)

#1 @apedog
15 months ago

You shouldn't have to modify WordPress core.
There's a filter hook on line 753:

$redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );

You can add your modified version of core's redirect_canonical() (or a function with your own logic) to that hook and save it in a plugin. Like so:

function my_custom_redirect_canonical( $redirect_url, $requested_url ){
    // your custom logic here
    return $redirect_url;
add_filter( 'redirect_canonical', 'my_custom_redirect_canonical', 10, 2 );

It should keep working whenever you upgrade WordPress core.

This is not to say that your report is, or isn't, a bug.
The strong coupling to $_SERVER superglobal is regrettable.

#2 @vladop
15 months ago

Your idea is to copy wordpress core's almost 700 lines of function add mine two lines and call that a plugin. And maintain it with each release of wordpress? :D

#3 @vladop
15 months ago

Also applying any code at that line where hook should go it seem that is too late. Becuase redirect loop happens again

Last edited 15 months ago by vladop (previous) (diff)
Note: See TracTickets for help on using tickets.