new();   # This is the wrong way to redirect # because the URL is missing the domain # information: print $query->redirect( "/site/admin/index.shtml" );   __END__ ```   Notice that with the wrong approach, the address bar displays the program that initiated the redirect. If the user refreshes their browser, this program will be called again, which isn't ideal. Users see a dialog box (in IE) upon refreshing with this type of redirect, leading to a subpar user experience and potentially unforeseen consequences for the underlying database.   Now, here's the right way to redirect:   ```perl #!c:\Perl\bin\perl.exe   use strict; use warnings;   use CGI; my $query = CGI->new();   # This is the right way to redirect # because we use a full URL print $query->redirect …" /> new();   # This is the wrong way to redirect # because the URL is missing the domain # information: print $query->redirect( "/site/admin/index.shtml" );   __END__ ```   Notice that with the wrong approach, the address bar displays the program that initiated the redirect. If the user refreshes their browser, this program will be called again, which isn't ideal. Users see a dialog box (in IE) upon refreshing with this type of redirect, leading to a subpar user experience and potentially unforeseen consequences for the underlying database.   Now, here's the right way to redirect:   ```perl #!c:\Perl\bin\perl.exe   use strict; use warnings;   use CGI; my $query = CGI->new();   # This is the right way to redirect # because we use a full URL print $query->redirect …"/> Clarkson Energy Homes – Effective Redirects: A Quick Guide

Effective Redirects: A Quick Guide

Posted on Fri 18 March 2022 in Programming

I'll admit, this might serve as a handy reminder for me more than anything else, but let's dive in anyway.

 

Recently, I delved into the world of PHP 5.x and its header() function. During my exploration, I stumbled upon an interesting piece of information:

 

"HTTP/1.1 requires an absolute URI as an argument to the 'Location:' header, including the scheme, hostname, and absolute path, but some clients accept relative URIs."

 

Now, I don't know about you, but RFCs (Request for Comments, essentially internet standards documents) can be incredibly dull. So, I'm grateful to the sleep-deprived souls who unearthed this gem for the rest of us.

 

Following this standard doesn't just matter for PHP; it can also solve issues in other languages, like Perl. In Perl, many folks wisely use CGI.pm for handling redirects, and the CGI manual dishes out the same advice: utilize full URLs for redirects.

 

Here's a mini guide to redirection (basically, you just print out the redirect):

 

```perl

#!c:\Perl\bin\perl.exe

 

use strict;

use warnings;

 

use CGI;

my $query = CGI->new();

 

# This is the wrong way to redirect

# because the URL is missing the domain

# information:

print $query->redirect( "/site/admin/index.shtml" );

 

__END__

```

 

Notice that with the wrong approach, the address bar displays the program that initiated the redirect. If the user refreshes their browser, this program will be called again, which isn't ideal. Users see a dialog box (in IE) upon refreshing with this type of redirect, leading to a subpar user experience and potentially unforeseen consequences for the underlying database.

 

Now, here's the right way to redirect:

 

```perl

#!c:\Perl\bin\perl.exe

 

use strict;

use warnings;

 

use CGI;

my $query = CGI->new();

 

# This is the right way to redirect

# because we use a full URL

print $query->redirect( "example.com/site/admin/index.shtml" );

 

__END__

```

 

This time, the address bar displays the address of the page we're redirecting to. If the user refreshes their browser, this page will be called again, providing a smoother user experience.

 

Using full URLs in redirects is not only a better practice but also aligns with the HTTP 1.1 standard. And as a rule of thumb, sticking to standards often pays off handsomely.