Apache rewrite rule to send all urls from site A to analogous url on siteB

Alec Jacobson

May 19, 2016

weblog/

Here's the rewrite rule I'm putting on my cs.columbia.edu site's .htaccess file to redirect everything to my new cs.toronto.edu site:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.cs.columbia.edu$ [NC]
RewriteRule ^(.*)$ http://www.cs.toronto.edu/~jacobson/$1 [R=301,L]

Now, not only does http://www.cs.columbia.edu/~jacobson redirect to http://www.cs.toronto.edu/~jacobson/, but so do more complicated urls: http://www.cs.columbia.edu/~jacobson/images/alec-jacobson.jpg --> http://www.cs.toronto.edu/~jacobson/images/alec-jacobson.jpg

This also works for <img src= tags:

face

Debugging .htaccess commands is made especially tedious because browsers like Google Chrome and Safari cache old .htaccess files. So it's easy to be tricked into thinking your rewrite rules aren't working. Remember to clear your cache between attempts (or use incognito windows each time).