WordPress.org

Support

Support » Plugins and Hacks » Smart 404 causes redirection loop with ?p= parameter

Smart 404 causes redirection loop with ?p= parameter

Viewing 14 replies - 1 through 14 (of 14 total)
  • Daedalon
    Participant

    @daedalon

    The same happened on my WP 3.2.1 and Smart 404 0.5. When typing a URL and adding ?p=anything in the end, I get a browser warning page of endless redirects.

    I don’t see my own blogs creating links like that, but this issue definitely sounds worth a fix.

    Graham Stoney
    Participant

    @greyham

    Well it sure is confusing to change default behavior given there’s no 404 involved.

    I’m not sure if the redirection loop is coming from the template_redirect action, or the redirect_canonical filter; or some interaction between the two. I can’t see an obvious bug in the Smart404 code, but I don’t claim to understand it. It’s almost as if is_404 is returning true for these requests and kicking Smart404 into action, when it should just be left to WordPress’s builtin redirect_canonical. The developer says he hasn’t got time to support this plugin any more; anyone interested in diving into this?

    Cheers,
    Graham

    Daedalon
    Participant

    @daedalon

    That would be explained by is_404‘s implementation:

    707	function is_404() {
    708	        global $wp_query;
    709
    710	        if ( ! isset( $wp_query ) ) {
    711	                _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
    712	                return false;
    713	        }
    714
    715	        return $wp_query->is_404();
    716	}

    In other words: if there are no conditional query tags, return false (that it is not 404). To understand why that makes sense I had a look at http://codex.wordpress.org/Class_Reference/WP_Query but didn’t have time to dig deep enough to find. It’s easy to guess why ?p=number causes a 404 (that’s what WP translates /page/number to before processing), but to find the exact cause and solution in the code needs more time than I have to spend right now.

    Graham Stoney
    Participant

    @greyham

    Thanks Daedalon,

    Hmmm… something about Smart404 seems to be making wp_query think it’s dealing with a 404 and calling set_404. Here’s a weird thing: the redirect loop has gone, but I’m now getting a 404 error on my live site for this URL:
    http://confidentman.net/confident-man-ebook/?p=112

    With Smart404 disabled, I get the post I expect (number 112).

    On my local XAMPP test site (which has slightly different content but most of the same settings), I still get the redirect loop.

    Fascinating captain…

    Daedalon
    Participant

    @daedalon

    404 Redirected together with Subscribe To Comments Reloaded suffer from a similar problem. STCR creates some sort of a virtual page, the URL for which the admin can choose in the settings. Then comment subscription management links are formatted as [SITE]/manage-subscriptions/?srp=[ID] where [ID] is the post/page in question.

    With 404 Redirected’s automatic redirections enabled, the link gets redirected to address [SITE]/[SITE]/?page_id=9999999/, the site being entered twice. Without the automatic redirection, subscription management page loads without any 404 errors.

    Like Smart 404, 404 Redirected uses is_404. Could that function have a bug causing both of these cases?

    Jason Crouse
    Participant

    @coolmann

    You may want to create a “real” management page to make 404R happy. Please take a look at the FAQs to see how to do that. This has been added just for cases like yours, where other plugins are not able to properly handle virtual pages 🙂

    Cheers
    Camu

    Daedalon
    Participant

    @daedalon

    Correction, the problem was resolved when I turned off the following option in the settings of 404 Redirected: “Force current permalinks:
    Creates auto redirects for any url resolving to a post/page that doesn’t match the current permalinks”. Otherwise the automatic redirects are now active and Subscribe To Comments Reloaded functions as expected.

    Thanks a lot for your help with Subscribe To Comments Reloaded, camu. Appreciated!

    Graham Stoney
    Participant

    @greyham

    Hmmm… my best guess is that WP_Query treats URLs like the original one I posted:
    http://confidentman.net/confident-man-ebook/?p=112

    … as 404’s internally in order to redirect them to honor the ?p=112 bit. When Smart 404 overrides WordPress’s built-in redirection it breaks with either a loop or a real 404.

    Jason Crouse
    Participant

    @coolmann

    A vote for my plugin would be a nice way to say thank you 🙂

    Camu

    Daedalon
    Participant

    @daedalon

    Graham, when I have on both Smart 404 and 404 Redirected with the setting Create automatic redirects, the problem you mentioned disappears. This seems both a suitable workaround for your case and a lead on where to find the code example to fix Smart 404 when used without 404 Redirected.

    Daedalon
    Participant

    @daedalon

    Strange, while turning the options and plugins on and off to seek the exact cause while looking at the plugin code, it stopped working. I continued debugging and found out the following oddities about both 404 Redirected and Smart 404 without requiring the other to be activated at the same time. May not apply with permalink structure of /?p=postnumber.

    With 404 Redirected activated, regardless of Smart 404:

    /?p=[anynumber] is shown in logs as a 404 error with the intended target URL /, and is redirected to /?random.

    /anypostname/?p=[anynumber] redirects to the URL so that ? and everything after it is removed. Essentially same as above.

    These work when either the page URL ending in / has been visited shortly before with Create automatic redirects on. After the first visit, this option can be turned off and the URL + ?p=[number] will still redirect to it.

    Examples: When setting on, visit /postname/ or /postname/?p=anynumber, no matter if you turn the setting then off or leave it on, visit /postname/?p=anyothernumber, you get redirected to /postname/. When setting off, visit /anotherpostname/ or /postname/?p=anynumber, no matter if you turn the setting then on or leave it off, visit /anotherpostname/?p=anyothernumber, you get a 404 error or if Smart 404 is on, you get an endless redirection loop. See more below.

    If the URL variable is anything else than ?p=anynumber, the results are different. Not worth analyzing in this post, as possibly any trouble with them would be solved at the same time as the above.

    With 404 Redirected deactivated and Smart 404 deactivated:

    /?p=[number] redirects to the post with that number, if one exists, if not, gives a 404 error message.

    /postname/?p=[number] also redirects to the post with that number, if one exists, if not, gives a 404 error message.

    With 404 Redirected deactivated and Smart 404 activated:

    /?p=[number] redirects to the post with that number, if one exists, if not, gives a 404 error message. Just as with Smart 404 deactivated. But:

    /postname/?p=[number] gives the endless redirection loop Graham described in his original post.

    Jason Crouse
    Participant

    @coolmann

    It seems like a problem with 404 Redirect.

    Daedalon
    Participant

    @daedalon

    In the last case that plugin is disabled, Smart 404 is activated and that causes an endless redirection loop, so it should be solved in Smart 404 or – this is a long shot – in core.

    Graham Stoney
    Participant

    @greyham

    Thanks for looking deeper into this Daedalon. 404 Redirected looks like a viable replacement for Smart 404, although it also breaks the core behavior. To eliminate confusion, I’ve asked the question with respect to 404 Redirected here.

    Thanks,
    Graham

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Smart 404 causes redirection loop with ?p= parameter’ is closed to new replies.