Category: "HTTP/SSL"

Live HTTP Header tracking for Firefox

Live HTTP Header tracking for Firefox

LiveHTTPHeaders is a FireFox extension that lets you look at all HTTP headers for all requests issued by the browser.

I am updating and resurfacing this post today since -- after a long while of being mildly maintained -- this fine plugin seems to have effective backing again. Version 0.17 works on FireFox 9.x.

Even in the days of FireBug, LiveHTTPHeaders is still an irreplaceable tool in a web developer's toolkit. As a matter of fact, LiveHTTPHeaders is the best way to track chains of redirects, which FireBug doesn't display right, as it will often clear the "Net" tab from one request to the next.

The header tracking window can be opened through Tools > Live HTTP Header. It will then capture all requests and display HTTP headers being sent as well as headers being received. Note that it is possible to stop capture at any time so the display stops scrolling. It is also possible to filter out specific requests (for example images) with a regular expression (regexp).

The extension also extends the Page Info windows with a Headers tab, which is useful if you just want to see the headers for the current page.

Finally, it's also possible to edit requests and replay them again, modified. At this time this is still in beta though, and Tamper Data may be a better extension for that purpose.

How to log request processing times in Apache

If you have an apache (2) web server, you probably have an access.log file showing you all kinds of data using the “combined” log format. Let’s see how to include processing time into that log file.

By default a line in the combined log looks like this:

[codeblock lang="” line="1″]10.0.209.80 - - [03/Dec/2009:03:20:47 +0100] “GET /info-tech/ HTTP/1.1″ 200 46482 “http://fplanque.com/info-tech/” “Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5″[/codeblock]

Notice the 2 dashes - - just after the IP. The first one stands for “I could not identify the user using ident” and the second one for “no user authentication was performed".

Now, let’s face it: you will never identify anyone using ident. Your apache conf probably even doesn’t try. That field is a left over from ancient times. So let’s replace that first dash with something useful, i-e: the processing time of the request! Note that by doing this we keep the global structure of the file identical and any log processing tool you might be using should not be affected.

Read more »

The importance of URIs

“People don’t recognize how important URIs are. The notion that you have a huge, world-scale, information space, and that everything in it has an name and they’re all just short strings that you can paint on the side of a bus; that’s a new thing and a good thing.”
Tim Bray

How to redirect www.domain.com to domain.com (or reverse)

Most sites can be reached by two different URLs. For example http://b2evolution.net/about/features.html and http://www.b2evolution.net/about/features.html .

This is good for the users who can choose whether or not to type in the "www." part. However, it is not good for search engines which will tend to see a lot of duplicate contents.

If you are on an Apache webserver, you can use mod_rewrite in order to redirect all traffic that goes to the www.domain to the "non www" domain. (If you want it the other way round, see below)

Try adding this to a file named ".htaccess" at the root of your site (create that file if it doesn't exist):

Code

RewriteEngine on
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{HTTP_HOST}   !^your-domain\.com [NC]
RewriteRule ^/(.*)         http://your-domain\.com/$1 [L,R=301]

Note: More advanced users will prefer to add this to the httpd.conf / apache2.conf instead in order to gain a little efficiency and also to not interfere with tests on development/staging servers.

Explanations

RewriteEngine on activates mod_rewrite.

The first RewriteCond makes sure we have a non(!) empty(^$) hostname to work with.

The second RewriteCond detects that the host name is not(!) the one we want (e-g: it has an extra www. part in it).

The RewriteRule sends out a permanent(301) redirection to the right domain name followed by the currently requested path/page ($1).

Handling multiple domains at once

Below is more elegant (yet complex to read) solution that can handle multiple domains at once (if you have "ServerAlias"es) and that doesn't require to type-in your domain (just copy/paste):

Code

RewriteEngine on
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{HTTP_HOST}   ^www\.(.+)$ [NC]
RewriteRule ^/(.*)         http://%1/$1 [L,R=301]

The trick here is to use %1 which matches the canonical part of the domain name in the second RewriteCond (.+) .

Reverse redirection

By popular demand, here is how to redirect from somedomain.com to www.somedomain.com

Code

RewriteEngine on
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{HTTP_HOST}   !^www\. [NC]
RewriteRule ^/(.*)         http://www.%{HTTP_HOST}/$1 [L,R=301]

PHP Output buffering and ob_handlers

PHP has some nice features to do output buffering. You just call ob_start() and anything you output (except headers) does not actually get set back to the webserver (and thus the browser) until you decide it. As the manual puts it "This can be useful in several different situations, especially if you need to send headers to the browser after your script has began outputting data."

One particular use of this feature is to pass an "ob_handler" callback function as an argument to ob_start(). This is supposed to let you do "kewl" post-processing operations like: gzip compression, adding an ETag, adding content-length of your whole output.

Crap!

There are only two situations where ob_handlers are useful:

  • When your webserver can't take care of this for you. But you know, you should really consider switching to Apache, which already has modules to handle all that, and even better: unlike PHP ob_handlers, it can do gzip compression progressively without waiting for the whole page to have generated! :crazy:
  • When you are in desperate need for marketing arguments to write a book on ~` Advanced PHP '~ ! >:XX