1

There's an odd behaviour I just noticed during some tests we're doing on one of our sites. We're verifying if the user isn't logged in and perform a wp_redirect in our theme's header.php like this:

if(is_page('gestionar-mentorias')){
  $post_id = get_the_ID();
  if(!members_can_current_user_view_post( $post_id ) && !is_user_logged_in()){
    wp_redirect(
      get_permalink( get_page_by_path( 'ingresa' ) ).'?redirect_to='.get_permalink( get_page_by_path( 'gestionar-mentorias' ) ) 
    );
    exit;
  }
} ?>

This seems to be working perfectly both in our local and testing environments. However, the production site (which isn't under our control) doesn't do the redirect some of the times!

I was trying to debug this when I came across an odd behaviour. If I do a curl -I http://fedesoft.simianlab.com/gestionar-mentorias/, the response is:

HTTP/1.1 200 OK
Date: Mon, 18 Apr 2016 23:47:00 GMT
Server: Apache/2.2.15 (Red Hat)
X-Pingback: http://fedesoft.simianlab.com/xmlrpc.php
Link: <http://fedesoft.simianlab.com/wp-json/>; rel="https://api.w.org/"
Link: <http://fedesoft.simianlab.com/?p=503>; rel=shortlink
Link: <http://fedesoft.simianlab.com/wp-json>; rel="https://github.com/WP-API/WP-API"
Content-Type: text/html; charset=UTF-8
Vary: Accept-Encoding

However, a curl -v http://fedesoft.simianlab.com/gestionar-mentorias/ renders this result:

*   Trying 52.3.201.45...
* Connected to fedesoft.simianlab.com (52.3.201.45) port 80 (#0)
> GET /gestionar-mentorias/ HTTP/1.1
> Host: fedesoft.simianlab.com
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 302 Found
< Date: Mon, 18 Apr 2016 23:47:09 GMT
< Server: Apache/2.2.15 (Red Hat)
< X-Pingback: http://fedesoft.simianlab.com/xmlrpc.php
< Link: <http://fedesoft.simianlab.com/wp-json/>; rel="https://api.w.org/"
< Link: <http://fedesoft.simianlab.com/?p=503>; rel=shortlink
< Link: <http://fedesoft.simianlab.com/wp-json>; rel="https://github.com/WP-API/WP-API"
< Location: http://fedesoft.simianlab.com/ingresa/?redirect_to=http://fedesoft.simianlab.    com/gestionar-mentorias/
< Content-Length: 0
< Content-Type: text/html; charset=UTF-8
<
* Connection #0 to host fedesoft.simianlab.com left intact

Reading the man page for cURL, I saw that the difference between both commands is that curl -I performs a HEAD request, while curl -v is doing a normal GET request.

According to the spec, the HEAD method "[...] is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request."

My question is, then. why do I get two different responses?

1
  • Replace wp_redirect() with wp_safe_redirect() and let me know if you're still having the issue. If you read the code it's recommended to use the wp_safe_redirect if you are redirecting to a page within your own site. Aug 23, 2016 at 13:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.