Reputation: 141
When I run my redirect cgi script (Perl), I just do the following:
print "Location: /new/url\n\n";
This works, but by default, it says Status:200 OK. If I specify the status like 302 or 301 in the header, then the browser redirects. Otherwise, it still loads the content of the new url without changing the URL in a browser.
I would just like to understand how a browser processes a Location: Header but has 200 status and what are pros and cons of doing this instead of using 302 or 301?
Either a feedback or a link to an article discussing this would be great. Thanks.
Upvotes: 1
Views: 435
Reputation: 69284
Make your life easier by using the redirect()
subroutine from CGI.pm.
$ perl -MCGI=redirect -E'say redirect("https://example.com")'
Status: 302 Found
Location: https://example.com
Upvotes: 1
Reputation: 123380
I would just like to understand how a browser processes a Location: Header but has 200 status and what are pros and cons of doing this instead of using 302 or 301?
A Location
header causes only a redirect when combined with some 3xx code. This behavior is clearly defined in the HTTP standard. From RFC 7231 7.1.2 Location:
For 201 (Created) responses, the Location value refers to the primary resource created by the request. For 3xx (Redirection) responses, the Location value refers to the preferred target resource for automatically redirecting the request.
In other words: it makes no sense at all to use a Location
header together with a response code 200. The Location
header will simply be ignored by the browser in this case.
Upvotes: 3