You could also use the following code:
header("Location: //www.google.com");
Answer from user1756209 on Stack OverflowPHP
php.net › manual › en › function.header.php
PHP: header - Manual
Found on https://en.wikipedia.org/wiki/HTTP_location «An obsolete version of the HTTP 1.1 specifications (IETF RFC 2616) required a complete absolute URI for redirection.[2] The IETF HTTP working group found that the most popular web browsers tolerate the passing of a relative URL[3] and, consequently, the updated HTTP 1.1 specifications (IETF RFC 7231) relaxed the original constraint, allowing the use of relative URLs in Location headers.» ·
PHP header location to https - Stack Overflow
Instead use the HTTPS scheme to access this URL, please. What do I have to change? What did I do wrong or what do I have to add? ... This sounds absurd but after some thinking: I added a slash (/) at the end so now it looks like this header('Location: '.PROJECT_HTTP_ROOT.'/');. More on stackoverflow.com
Redirecting from HTTP to HTTPS with PHP - Stack Overflow
I'm working on a shopping cart website and I would like to redirect the user to a HTTPS page when he's entering his billing details and maintain the HTTPS connection for the next pages until he log... More on stackoverflow.com
PHP header(location) function not working
Once you output any content (an echo statement, or white space or text outside of PHP), then you can no longer modify the header information. Someone posted a similar problem recently, with a link to a good explanation on Stack Overflow: https://teamtreehouse.com/forum/redirecting-after-a-... More on teamtreehouse.com
$_SESSION data lost after header redirect
deleted 0.0128 What is ^^^this? More on reddit.com
Top answer 1 of 15
96
Try changing:
header("Location : blabla")
^
|
(whitespace)
To
header("Location: blabla")
2 of 15
49
Well, if the server sends a correct redirection header, the browser redirects and therefore "changes the url". It might be a browser issue, then. I don't know if it has anything to do with it, but you should not send a relative url in the location header ("HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. ", http://php.net/manual/en/function.header.php), and "location" must be capitalized, like:
header('Location: http://myhost.com/mypage.php');
Edureka
edureka.co › blog › header-location-in-php
Header Location In PHP | PHP Header Location Edureka
February 25, 2025 - It must be called before sending any actual output, either by normal HTML tags, blank lines in a file or from a PHP file. Syntax: header(string,replace,http_response_code); string: It consists of a header string. Basically, there are two types of header calls. One is header which starts with string “HTTP/” used to figure out the HTTP status code to send. Another one is the “Location” which is mandatory.
HubSpot
blog.hubspot.com › home › website › how to redirect in php: what you need to know
How to Redirect in PHP: What You Need to Know
May 13, 2025 - First, remember where you place the header() function in your index.php file isn’t arbitrary. Second, recall that you may choose to set HTTP status codes. These will control how the server redirects a search engine or user. In order for your PHP redirect to be successful, the header() function must execute — and before any output is sent.
PHPpot
phppot.com › php › php-header
PHP header - PHPpot
The header location redirects users to the target URI and returns the 302 status code to the client. It is to replace the header property already defined. This is an optional parameter having boolean true as a default value. It overrides the defined header settings by default. We can turn the default behavior off by setting false to this parameter. <?php header('WWW-Authenticate: Negotiate'); echo ('header WWW-Authenticate is set to Negotiate \n'); header('WWW-Authenticate: NTLM', false); echo ('header WWW-Authenticate is changed to NTLM'); ?>
GeeksforGeeks
geeksforgeeks.org › php › php-header-function
PHP | header() Function - GeeksforGeeks
July 11, 2025 - The second case of header is the "Location:". It is mandatory parameter. $replace: It is optional parameter. It denotes the header should replace previous one or add a second header. The default value is True (will replace). If $replace value is False then it force multiple headers of the same type. $http_response_code: It is an optional parameter. It forces the HTTP response code to the specified value (PHP 4.3 and higher).
DaniWeb
daniweb.com › programming › web-development › threads › 358490 › header-location-php-help
Header location php (help) | DaniWeb
April 8, 2011 - You were tripping over three separate issues: 1) misuse of the header() call (it should send a plain URL string — not an HTML anchor), 2) session_start() / header() must run before any browser output (including stray whitespace or a BOM), and 3) base64/urlencode only obfuscates a link — it does not protect it. @cwarn23 already pointed out the <?php vs <? and header/output ordering, and @happytogether’s ob_start() is a workable fallback; the long-term fix is to eliminate any output before session or header calls and save files as UTF‑8 without BOM.
W3Schools
w3schools.in › php › php-headers
PHP Header Function - W3Schools
<?php header("Location: http://www.example.com/"); ?>
Tutorialspoint
tutorialspoint.com › php › php_function_header.htm
PHP Network header() Function
Below is the syntax of the PHP Network header() function − · void header ( string $header, bool $replace, int $res_code ) Here are the parameters of the header() function − · $header − (Required) The exact header text to send, like "Location: URL" or "HTTP/1.1 404 Not Found."
W3Schools
w3schools.com › php › func_network_header.asp
PHP header() Function
Let the user be prompted to save a generated PDF file (Content-Disposition header is used to supply a recommended filename and force the browser to display the save dialog box): <?php header("Content-type:application/pdf"); // It will be called downloaded.pdf header("Content-Disposition:attachment;filename='downloaded.pdf'"); // The PDF source is in original.pdf readfile("original.pdf"); ?> <html> <body> ...
Wikipedia
en.wikipedia.org › wiki › HTTP_location
HTTP location - Wikipedia
October 17, 2025 - The HTTP Location header field is returned in responses from an HTTP server under two circumstances: To ask a web browser to load a different web page (URL redirection). In this circumstance, the Location header should be sent with an HTTP status ...
Top answer 1 of 6
289
Try something like this (should work for Apache and IIS):
if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === "off") {
$location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $location);
exit;
}
2 of 6
31
This is a good way to do it:
<?php
if (!(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' ||
$_SERVER['HTTPS'] == 1) ||
isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'))
{
$redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $redirect);
exit();
}
?>
Content-Security-Policy
content-security-policy.com › examples › php
Content-Security-Policy PHP Examples
Here's how to use PHP to add a Content-Security-Policy HTTP response header to your site.
Hostinger
hostinger.com › home › tutorials › how to make a php redirect
How to make a PHP redirect
November 19, 2025 - To perform a redirect in PHP with specific HTTP status codes like 301 or 302, you add the status code as an optional parameter to the header() function. By default, header(‘Location: …’) sends a 302 temporary redirect. To add a specific code, you use the function’s optional parameters: header(“Location: URL”, true, 301).
Top answer 1 of 15
2
Once you output any content (an echo statement, or white space or text outside of PHP), then you can no longer modify the header information.
Someone posted a similar problem recently, with a link to a good explanation on Stack Overflow:
https://teamtreehouse.com/forum/redirecting-after-a-form-submission-not-functioning
2 of 15
2
Ok, I see...
Maybe you have printed out some text before the header() function, maybe a blank space.
Check the first line of your .php file does not have a blank space, something like this:
--- Blank space ---