0

What is the best method to find where a URL redirects to?

If using cURL CURLOPT_FOLLOWLOCATION will follow every header. What I am looking to do is just find the next location?

My first idea was to use cURL but disable FOLLOWLOCATION and then parse the result for Location: XXXX but I was thinking there must be a more efficient way.

3
  • "More efficient?" That's the definition of where a URL redirects to. Commented Oct 5, 2015 at 20:40
  • But I would essentially just be scraping the header. Is there a way to natively do it in PHP/cURL? Commented Oct 5, 2015 at 20:41
  • Yes, I added an answer below. Commented Oct 5, 2015 at 20:46

1 Answer 1

2

You can set CURLOPT_MAXREDIRS (see manual) to 1 along with CURLOPT_FOLLOWLOCATION.

However, note that unless combined with CURLOPT_HEADER, this will result in grabbing the contents of the resource being redirected to. You don't want to do that just to find where the redirect goes.

For all that it's worth, IMO in this situation it would be better and very comparable complexity-wise to parse the Location header of the response instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.