Fun with .htaccess

| posted in: life 


Recently I have made good use of the Apache .htaccess file to redirect and rewrite URL’s to keep visitors from getting page not found errors. Moving three domains, creating a subdomain or two, and then moving things around again, has created ample opportunity for lost visitors.

There are lots and lots of pages in the Google search results for “.htaccess,” so here are my notes, all in one place, for your reading pleasure.

Redirect visitors from an old site to a new site

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

Redirect visitors using .html links to .php links

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)\.html$ http://newdomain.com/$1\.php [L,R=301]
RewriteRule ^(.*)\.php$ http://newdomain.com/$1\.php [L,R=301]
RewriteRule ^(.*)$ http://newdomain.com/ [L,R=301]

Author's profile picture

Mark H. Nichols

I am a husband, cellist, code prole, nerd, technologist, and all around good guy living and working in fly-over country. You should follow me on Mastodon.