Last month, I published two articles that talked about using cURL to handle redirects that may inevitably happen when working with certain URLs.
Specifically, I’m talking about:
- Finding the Destination of a Redirect with PHP
- Using cURL to Determine If the Specified URL Is a Valid Page
And though the second one is more of the subject of this post, I wanted to reference both since they are related.
Earlier this month, I wrote a bit how the purpose of blogging has changed. Perhaps it would’ve been better to talk about the motivation rather than the purpose, but I digress.
In this post, I talk a bit about commenting and feedback. And since I’ve closed comments, one of the ways that people will talk with me about certain posts is via Twitter.
Case in point:
Isn’t it better to return 200 === $httpCode as there are other possible status codes too?
— Xaver (@xaver_) May 30, 2019
And I like this because it’s:
- a clearly stated, succinct question,
- it’s directed towards me (with the potential for others to chime in),
- and it can keep the conversation on the topic without it devolving into something else in the comments.
Further, Xaver’s question is good because it shows where my content may be lacking, and it gives me the opportunity to write a follow-up or a clarification on a post like this 🙂.
The thing is, the response to this particular question may not be as long as the lead in, but I always want to give enough context before providing an answer.
Blog Feedback and Rationale for Design Decisions
So with that said, the short answer is that checking from the status code of the result of the URL in question is a good way to test if the specified URL can be reached.
That is, 200 is literally a status code for OK.
However, it doesn’t always mean that the URL that it’s first hitting is the final destination on which you’ll land.
Ultimately, the goal is two-fold:
- Make sure the final destination returns a 200,
- Determine what the final destination of a specified URL is.
And the second point is important because so many sites – such as Twitter – use redirects as part of their analytic service to track clicks and who-knows-what-else as we’re browsing the web after leaving their service.
Further, there are simply times in which we may be given a URL for, say, a link to inventory on a shop where the destination URL is different than the original URL we’re given.
I’ve experienced this first hand in a project on which I’m working which is the whole motivation behind this original post.
So checking for your HTTP statuses is a good check but if you’re looking to detect if the final destination of a redirect is valid, it will take a little bit more.
Of course, that all depends on the context of what you’re working on, doesn’t it? For some, checking status codes is great. For others, it may need a little more.