WordPress.org

Ready to get started?Download WordPress

Forums

Smart 404
Smart 404 causes redirection loop with ?p= parameter (15 posts)

  1. Graham Stoney
    Member
    Posted 2 years ago #

    I recently posted a question in the WordPress support forums asking how to override a WordPress page address by adding a ?p= parameter to it, to target ClickBank affiliate hoplinks. After a little more testing I've discovered that WordPress already supports exactly the functionality I want, but the Smart404 plugin causes a redirection loop when I use it.

    With Smart404 disabled, a link like this:
    http://confidentman.net/confident-man-ebook/?p=112

    Displays:
    http://confidentman.net/?p=112

    Instead of:
    http://confidentman.net/confident-man-ebook/

    Which is exactly the behaviour I want.

    But with Smart404 enabled on my site a get an error message from Firefox saying: "The page isn't redirecting properly. Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

    I have my permalinks set to: /%category%/%postname%

    Anyone else noticed a redirection loop using Smart404 with ?p= ?

    Thanks,
    Graham

    http://wordpress.org/extend/plugins/smart-404/

  2. Daedalon
    Member
    Posted 2 years ago #

    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.

  3. Graham Stoney
    Member
    Posted 2 years ago #

    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

  4. Daedalon
    Member
    Posted 2 years ago #

    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.

  5. Graham Stoney
    Member
    Posted 2 years ago #

    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...

  6. Daedalon
    Member
    Posted 2 years ago #

    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?

  7. camu
    Member
    Posted 2 years ago #

    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

  8. Daedalon
    Member
    Posted 2 years ago #

    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!

  9. Graham Stoney
    Member
    Posted 2 years ago #

    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.

  10. camu
    Member
    Posted 2 years ago #

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

    Camu

  11. Daedalon
    Member
    Posted 2 years ago #

    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.

  12. Daedalon
    Member
    Posted 2 years ago #

    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.

  13. camu
    Member
    Posted 2 years ago #

    It seems like a problem with 404 Redirect.

  14. Daedalon
    Member
    Posted 2 years ago #

    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.

  15. Graham Stoney
    Member
    Posted 2 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic