Check 301 redirects in htaccess file

This is post is now quite old and the the information it contains may be out of date or innacurate.

If you find any errors or have any suggestions to update the information please let us know or create a pull request on GitHub

Often you will come across a situation where you have to do a mass of redirects, often when moving platform e.g. from osCommerce to Magento.

So you might create a whole load of .htaccess rules which you want to verify, either before or after implementing them.

Rules like : RewriteRule ^top-level-category/old-category/product-name-p-3944.html$ new-product-name.html

I wrote this bash snippet to do just that - here’s the validate the pages you are redirecting to are valid (200) :-

for f in cut -f3 -d' ' .proposed-htaccess; do curl -I -v http://www.mydomain.co.uk/$f 2>&1 > /dev/null | grep ‘HTTP/1.1 ’ | sed s/‘.*1.1 ‘// | grep 200 > /dev/null || echo $f failed to fetch done

Then, once you have got the .htaccess uploaded you can verify that they are indeed rewriting should you wish :-

for f in cut -f3 -d' ' .proposed-htaccess; do curl -I -v http://www.mydomain.co.uk/$f 2>&1 > /dev/null | grep ‘HTTP/1.1 ’ | sed s/‘.*1.1 ‘// | grep 301 > /dev/null || echo $f was not a 301. done