I guess this is more a reminder to myself than useful information, but here goes.

Recently, I read up on PHP 5.x header() function. I ran across this tidbit of info.

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

I don't know about you, but I find RFCs incredibly boring, so I'm glad that some insomniac took the time to find this gem.

Following this standard eliminates another problem in a different language. In Perl, many people correctly use CGI.pm to perform redirects. The CGI manual gives the same advice. Use full URLs to redirect.

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

PERL:
  1. #!c:\Perl\bin\perl.exe
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use CGI;
  7. my $query = CGI->new();
  8.  
  9. # This is the wrong way to redirect
  10. # because the URL is missing the domain
  11. # information:
  12. print $query->redirect( "/site/admin/index.shtml" );
  13.  
  14. __END__

Notice that the address bar shows the program which redirected here. This program will be called again if the user refreshes their browser.

The user sees this dialog box (in IE) if they refresh with the the redirect from above. This is a poor user experience and may result in unforeseen consequences for the underlying database. The redirect below doesn't show the dialog. It refreshes the intended page.

PERL:
  1. #!c:\Perl\bin\perl.exe
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use CGI;
  7. my $query = CGI->new();
  8.  
  9. # This is the right way to redirect
  10. # because we use a full URL
  11. print $query->redirect( "http://www.example.com/site/admin/index.shtml" );
  12.  
  13. __END__

Notice that the address bar now shows the page address which we redirect to. This page will be called again if the user refreshes their browser.

Using the first example (the wrong way) will result in loading the new URL, but will leave the calling URL in the browser address bar. More importantly, a browser reload reloads the Perl script, not the redirected page. This gives the user a dialog box about resubmitting page info.

Using full URLs in redirects results in a better user experience and follows the standard set by HTTP 1.1. Standards compliance is often a very good thing.

Digg!