Redirect in PHP

Redirect in PHP

Redirect in PHP means transfer users from one page of the website to another or forwarding URLs (uniform resource locator). It is mostly used to specify old traffic of a website should be transferred to another URL.

It is also used to transfer users and search engines to another URL from the original URL. There are three types of redirects

301 (move permanently)

302 (move temporarily)

Meta Refresh?

?

301 (move permanently)

It shows to the search engine that the page has moved permanently. It is used when you transfer your whole website to another domain.????????????????????????????????????????????

302 (move temporarily)

It is used for the temporary transfer of pages, URL,s or websites for example when we need some maintenance on our website. It is used for a temporary basis

Meta Refresh?

It is used to instruct a web browser to load the current web page after a specific counter of time automatically, for example: to transfer one page to another with a specific time frame.

Ways to Redirect in PHP

To transfer one page to another in PHP two basic ways are used

Using header function

By java script through PHP?

?

Using header function

Header function is a built-in function in PHP and it sends an HTTP header to a client or browser. When a web page is requested server sends the header function along with some additional details such as URL date, request date, etc. It is very important to know that the header function always calls before output.

Uses of header function

The header function is used for redirection purposes in PHP. It can be used to redirect from one page to another page. It can also be used to download a file from the server and store it into a directory of the user system. For example, when used within a file management system.

Parameters of header function??

Location

Refresh

Content-type

Content- disputation

Location

header(“location:url”);

Refresh

header(“refresh:time”);

Content-type

header(‘content-type:file/type’);

Content- disputation

header(‘content- disputation: attachment; file name.type’);

Examples of header function

<?php

// this is a PHP program to describe header function

??

// Set any date, time, month

header("Expires: mon, 24 may 2020 06:00:00 GMT");

header("Cache-Control: no-cache");

header("Pragma: no-cache");

?>

??

<html>

???<body>

?????<p>HELLO WORLD</p>

????

?????<!-- PHP program to display

?????header list -->

?????<?php

???????print_r(headers_list());

??????>

???</body>

</html>

OUT PUT?

HELLO WORLD

?

Array (

???[0] => X-Powered-By: PHP/7.0.33

???[1] => Expires: : mon, 24 may 2020 06:00:00 GM

???[2] => Cache-Control: no-cache

???[3] => Pragma: no-cache

)

Example?2

<?php

?header("Location: https://www.dumysite.com/");/* This is used to redirect the browser */

?echo("the Browser is successfully redirected to https://www.example_site.com/");

?exit;

??>

Example 3

<?php

?// it is used to output a PDF header

?header('Content-Type: application/pdf');

?// Let the filename be called data.pdf

?header('Content-Disposition: attachment; filename="data.pdf"');

?// Source of the PDF can be found in oldfile.pdf

?readfile('oldfile.pdf');

??>

?

There are a lot of issues that we are facing when we redirect one page to another page. Some of them are following.

Header already sent error

Server error

Header replacement

?

Header already sent error

It is the most simple error and most people face this type of error when they redirect a page or URL. The main cause of this error is that most of the time the programmer does not send a header before anything in the PHP program. The header should be always sent before anything else even a space or any character etc.??

<?php

echo ‘<html>’;

header(“location: /data.php”);

exit();

?>

In the program, the error is displayed because the <html> is used before the header function.

?

Server error

?

This error occurs when we use unnecessary spaces in the syntax. For example, when we use space between location and colon, this type of error occurs??

<?php

header(“location : data.php”);

exit();

?>

When this code executes error occurs because there is space in between the location and colon

?

What works is:

<?php

header(“location: data.php”);

exit();

?>??????????????????

This code executes without any error because there is no space between the location and the colon

?

Header replacement?

This type of error arises when some other entry is replaced with a another version of the header:

?

<?php

header(“location: page1.php”);

header(“location: page1.php”);

header(“location: page2.php”);

exit();

?>

?

?

By JavaScript through PHP?

?

This type is also used for the redirection of pages. JavaScript can be used for redirection purposes it is less frequent to see than the header function method but it is also an effective way of redirect.

Example

It consist of window.location function and href attributes along with URL you want to redirect and most important do not forget to add <script >

<?php

echo ‘

<html>

<Script type=”text/javascript”> window.location.rel1= “new “ target=” blank” href=\‘https://www.dumysite.com/\’;

</script>’;

?>

Advantages and disadvantages of redirection

There are the following advantages and disadvantages of redirect

Advantages

There are the following advantages of redirection:

Linking old pages to the new one

When you modify your website or move to some other platform and retain your position in a search engine, you can redirect your old URL to a new one.

Useful in marketing

Suppose you are running a marketing campaign and you're sending thousands of emails to your customers. Redirect helps you to solve an erroneous link to your website and land your customers on the correct page.

Short URL

It is used to short the URL, which may be easier to access than the original long address.

Pass page rank

Most URL redirection services mostly use 301 redirects (it is used for permanent redirection) . This brings the extra benefit of passing on the page rank to the destination URL. That makes the new page location more important to the search engine

Ads revenue

When your service becomes popular, you can monetize your website and get revenue with the help of ads that are shown on your website

Quickly move to a new domain without losing traffic

Sometimes you need to change your domain name. With the help of redirection, you will soon transfer to some other domain name without losing your old traffic.

?

Disadvantages

Phishing

Redirection is also used to lead people to a phishing sites using an address that is very similar to the original.

?

?

HOW TO check redirects?

There are a lot of different tools that are used to check the error and issues in redirects. Some of them are following

Link redirect trace?

Google search console

Content forest

?

Link redirect trace?

For checking any broken or incorrect redirect on your website, the chrome extension is used. If there is anything that is not SEO friendly on your site, this helps you to give you an alert that you need some changes to make your website SEO friendly.??

Google search console

This helps you to find issues related to 404 errors and correct them without searching manually on each page for this purpose you just log in to the google search console and can easily spot the issue thanks to google which found it when crawling your website.

Content forest

This allows you to copy and paste any URL and check redirects: it gives you code associated with the URL and a map of redirect so you can easily check the errors.

IMPORTANT TOPIC?

There are the following important topics which we will discuss:

Meta refresh

Techniques useful in a redirect for SEO

Mistakes using redirect for SEO

?

Meta refresh?

Meta refresh is a method of refreshing the current web page after a given interval of time and it is used for redirect purposes as well, the characteristic is the possibility of a specific time frame.

Syntax?

<?php

?

echo ‘<head>

?

<meta http-equiv=”refresh” content=”5;

URL=https://dumysite.com/”/>

</head>’;

In this example, after the 5 seconds, the page is moved to a specific URL

?

The following code is also used for the redirection of pages. In this type, JavaScript is used for redirection purposes, but this method is slower than the header function method.

?

Example

<html>???

??<head>????

???<title>TEST</title>?

???

???<meta http-equiv="refresh" content="2;URL='https://dumysite.com

/'" />???

??</head>????

??

??<body>?

???<p>The page is moved to <a

>

??

??dumysite.com</a>.</p>?

??</body>?

</html>???

Meta refresh drawbacks

Meta refresh is a less used redirect method if you compare it to another redirect method like PHP with HTTP status 301 and 302. Google also not recommended the Meta refresh in some instances.

If you wanted to redirect one file with multiple files

Spammers also use Meta refresh to fool the Search Engine.

Due to these reasons, this is spam and thus it is removed from their index

It can confuse users because when the website refreshes or redirect after some time users may think there is some security issue with the website.

Techniques useful in a redirect for SEO

There are the following technique which is useful for SEO

Escape Redirect Chain?

Clean redirect?

Redirect to relative URL

Redirect to the appropriate version of the URL??

Update links

?

Escape Redirect Chain?

We should not use redirect more than two in a row. It can cause

SEO issues. Always Review your redirects and combine them excluding the chains.

?

Clean redirect?

Sometimes we use redirect and forget about it. This may also cause SEO problems. Always make a change after some time, according to the website history; there are a lot of redirects that we use temporarily, forgetting about them is a mistake so always clean them when not needed.

?

Redirect to relative URL

Always redirect the original URL to the relevant URL.

When we do not redirect with relevant URL, it may also cause some SEO problems??

Redirect to the appropriate version of the URL??

I always prefer the most relived version of the URL. It is useful when website generate dynamic URL and have multiple URL for the web site

Update links

Regularly check your sitemap and make changes to the URL and always update links when required

?

Mistakes using redirect for SEO

The following mistakes happen when using a redirect which is not good for SEO

Transferring domain without setting of redirect first

Increase latency

?

Transferring domain without setting of redirect first

Sometimes some companies wanted to transfer their site to a new domain and they directly transfer to a new domain name without redirecting their new site with the old one and as a result of their old traffic has not come to the new site. They have to find new traffic, and it needs some time to resolve such type of issue. We need to redirect our new site with the old one, so our precious traffic will also come to our new site, which also is very helpful for SEO.

Increase latency?

Multiple redirects may also cause latency issues as we know redirect is a server request that is processed. High latency may cause slow connection, and the browser may take more time to process on your site, which is significantly harmful for SEO and very bad for the user experience because of the waiting time to process.??

?

Conclusion

Hopefully, from this article, you learned a lot about redirects and ways on how to redirect by using different languages like PHP, JavaScript, HTML, etc., and you may also understand the importance of redirection and uses of redirection and also know the steps to do for an efficient redirection. You have also got knowledge about redirect advantages and disadvantages and learn about the types of redirection like 301 302 and meta-refresh and also learn about issues that you face during redirection and how to resolve these issues and different ways to check your redirect error and resolve these errors with the help of google search console and content forest

etc., and you may also have learned about the techniques that are good for SEO and the mistakes that are not SEO friendly. So with the help of these techniques, your site will rank in Google, and you will get good traffic.

?We can learn to redirect briefly along with the script. Now you have to do the practice… and become a redirect expert. I hope you will like the article and learn a lot from it.

??

?


要查看或添加评论,请登录

社区洞察

其他会员也浏览了