PHP redirect to another website

Asked
Active3 hr before
Viewed126 times

10 Answers

websiteredirect
90%

Although this is a quick-and-dirty way to achieve what was originally asked, it would eventually turn out to be an SEO disaster, as this kind of redirect is always interpreted as a 301 / 302 redirect, hence search engines will always see your index page as a re-directed page, and not something of a landing page / main page.,When you click on the link above, you are automatically redirected to this page. Moreover, it is a permanent redirection (Status: 301 Moved Permanently). So, if you type the first URL into Google, you will automatically be redirected to the second, redirected link.,I've already answered this question, but I'll do it again since in the meanwhile I've learnt that there are special cases if you're running in CLI (redirects cannot happen and thus shouldn't exit()) or if your webserver is running PHP as a (F)CGI (it needs a previously set Status header to properly redirect)., is there any way to redirect before the browser detects the HTTP protocol? the reason i need to redirect is because i cannot get enough SSL certificates for all of my domains. i'd use .htaccess to redirect, but i need a way to somehow pass on which domain redirected to the final domain? – oldboy Aug 7 '19 at 0:53

You can use the header() function to send a new HTTP header, but this must be sent to the browser before any HTML or text (so before the <!DOCTYPE ...> declaration, for example).

header('Location: '.$newURL);

die() or exit()

header("Location: http://example.com/myOtherPage.php");
die();

This function doesn't incorporate the 303 status code:

function Redirect($url, $permanent = false) {
   header('Location: '.$url, true, $permanent ? 301 : 302);

   exit();
}

Redirect('http://example.com/', false);

This is more flexible:

function redirect($url, $statusCode = 303) {
   header('Location: '.$url, true, $statusCode);
   die();
}

As mentioned header() redirects only work before anything is written out. They usually fail if invoked inmidst HTML output. Then you might use a HTML header workaround (not very professional!) like:

 <meta http-equiv="refresh" content="0;url=finalpage.html">

Or a JavaScript redirect even.

window.location.replace("http://example.com/");
load more v
88%

Redirection from one page to another in PHP is commonly achieved using the following two ways:Using Header Function in PHP: The header() function is an inbuilt function in PHP which is used to send the raw HTTP (Hyper Text Transfer Protocol) header to the client. Syntax: ,PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.,Parameters: This function accepts three parameters as mentioned above and described below:  ,Below program illustrates the header() function in PHP:Program: 

Redirection from one page to another in PHP is commonly achieved using the following two ways:
Using Header Function in PHP: 
The header() function is an inbuilt function in PHP which is used to send the raw HTTP (Hyper Text Transfer Protocol) header to the client. 
Syntax: 

header($header, $replace, $http_response_code)

Output: 

URL: https: //ide.geeksforgeeks.org/tryit.php
   Hostname: ide.geeksforgeeks.org
Protocol: https:
load more v
72%

You can simply use the PHP header() function to redirect a user to a different page.,The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php. You can also specify relative URLs.,If the status code is not specified explicitly, for instance header("Location: URL") defaults to 302 (Found). For temporary redirect use the HTTP status code 307.,If you want to redirect the users from old page to a new page on a permanent basis then also mention HTTP response code in the header() function as shown in the following example, so that search engines transfer "page rank" from the old page to the new page.

< ? php
header("Location: http://www.example.com/another-page.php");
exit(); ?
>
load more v
65%

Where 'http://www.redirect.to.url.com/' is the URL you wish the users to be redirected to. This can also be a file, like so:,WordPress Maintenance Mode: Everything You Need to Know,Files can be of any type, including but not limited to HTML, python, PHP, CGI, Perl, and compiled CGI programs.,HostingShared HostingVPS HostingDedicated Hosting

< ? php header("Location: http://www.redirect.to.url.com/"); ? >
load more v
75%

Most guides will tell you that to make a PHP redirect you can just use the header() function at the top of your pages. To do that, you use the function to send a new URL, like this:,While following the steps above should mean that your PHP redirects execute securely, if you are in the position of using multiple PHP redirects, it is probably time to rethink the structure of your site.,And whilst you are catching up on your reading, make sure to also protect a website from common vulnerabilities: if you are already in the position of having to use PHP redirects, it’s likely that your site’s security will need an audit.,That’s why you have to stop processing the rest of the page, in case the redirection is ignored. The way to do that is to append die() or exit() after your redirect:

Most guides will tell you that to make a PHP redirect you can just use the header() function at the top of your pages. To do that, you use the function to send a new URL, like this:

header('Location: '.$newURL.php);
load more v
40%

First include sitefunctions.php and than call movePage() as follows:,Nginx Redirect URL With HTTP/1.1 301 Moved…,Create a sample function called movePage() in sitefunctions.php (note I’m not the author of the following I found it somewhere else on the Internet):,Using Javascript function

< ? php
/* Redirect browser */
header("Location: http://theos.in/");

/* Make sure that code below does not get executed when we redirect. */
exit; ?
>
load more v
22%

A PHP redirect is a server-side solution to forwarding users and search engines from one URL to another using the header() function. Since its server-side — as opposed to an HTML redirect, which is client-side — a PHP redirect provides faster and more secure navigation from one page to another.,PHP redirects can help users and search engines navigate smoothly and securely between pages on your site. The good news is setting up these redirects is easy thanks to the header() function.,There are some additional rules for using the header() function to set up a PHP redirect. First, where you place the header() function in your index.php file matters. Second, you can set HTTP status codes to control how the server redirects a user and search engine.,If the status code is not specified in the header function, then it defaults to 302. 302 indicates a temporary redirection. Meaning, the requested URL resides temporarily under a different URI.

 < ? phpheader('Location: http://www.example.com/');
 exit; ? >
load more v
60%

In PHP, when you want to redirect a user from one page to another page, you need to use the header() function. The header function allows you to send a raw HTTP location header, which performs the actual redirection as we discussed in the previous section., $header: This is the HTTP header string that you want to use. In our case, we’ll use the Location header for redirection.,Let's go through the syntax of the header() function.,The 301 permanent redirect allows you to inform the search bots that the page is no longer available, and it can be replaced with a new page.

Let's go through the syntax of the header() function.

header($header, $replace, $http_response_code)
load more v
48%

If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the » Content-Disposition header to supply a recommended filename and force the browser to display the save dialog. , PHP scripts often generate dynamic content that must not be cached by the client browser or any proxy caches between the server and the client browser. Many proxies and clients can be forced to disable caching with: , There are two special-case header calls. The first is a header that starts with the string "HTTP/" (case is not significant), which will be used to figure out the HTTP status code to send. For example, if you have configured Apache to use a PHP script to handle requests for missing files (using the ErrorDocument directive), you may want to make sure that your script generates the proper status code. , The HTTP status header line will always be the first sent to the client, regardless of the actual header() call being the first or not. The status may be overridden by calling header() with a new status line at any time unless the HTTP headers have already been sent.

load more v
23%

Redirection is trivial with PHP. All you need to do is to send the visitor's browser an HTTP header specifying the new location, and the browser will do the rest. This is done with the header() function. , If you are a newcomer to PHP, it may not be obvious to you that normal content has already been sent to the browser. Take the following buggy script as an example. , If you want maximum compatibility with old browsers, use an absolute URL for the redirection. That is, specify the web address in full, including the protocol (eg, "https://"). , Please note that this article specifically answers the question of how to use PHP for the redirection. It is useful if you are writing a PHP script that needs to direct a user to some other address after it's done some task, or as part of its operation.

How to Send an HTTP Header with PHP

Redirection is trivial with PHP. All you need to do is to send the visitor's browser an HTTP header specifying the new location, and the browser will do the rest. This is done with the header() function.

header()
load more v

Other "website-redirect" queries related to "PHP redirect to another website"