Posted on February 2, 2011 at 2:04pm EST. More.

Using curl to test .htaccess redirects

Say you want to use .htaccess URL rewriting to make sure that always redirects to (or vice-versa). Or something more complicated that depends on the host name.

You want to test it before you upload to your production server, but your staging server has a different URL (obviously).

An easy trick is to use curl, manually setting the Host: header to simulate access through different URLs. For example, assume your test server is at IP address and you want to make sure that redirects to Type this:

curl --head --header "Host:"

The –head option fetches only the headers. The –header option spoofs the Host: header with the value that would have been sent by a browser accessing your production site.

If your .htaccess is correct, you should get output like this:

HTTP/1.1 301 Moved Permanently
Date: Wed, 02 Feb 2011 18:47:54 GMT
Server: Apache/2.2.14

If the HTTP code on the first line is 301 and you have a Location field with the expected URL, it worked!

If you see “HTTP/1.1 200 OK” then the server just served up a regular page. If you see “HTTP/1.1 500 Internal Server Error” then something went horribly wrong.

Armed with this knowledge, a clever person could write a script to automate these tests, confirming that all the redirects work as expected before deploying the site.