By
04/18/2012   Comments Off on .htaccess file bits

View all snippets

Here’s a few codes I’ve picked up over the years for the powerful .htaccess file. These work only for Apache Linux servers:

Redirection:

If you want to redirect all pages of an entire site to the main page of another domain, add the following code to the file, on a single line.

RedirectMatch 301 (.*) http://www.yournewdomain.com/

If you want to redirect one page to another page, add the following code to the file, on a single line.

redirect 301 /old-file-name.htm http://new-websites.com/new-file-name.htm

If you want to redirect all non-www requests to your site to the www version, all you need to do is add the following:

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

Do it in reverse:

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

Make sure to replace “my-domain” and other sample addresses with your own.


Make sure the character set is explicit on the server

utf-8 is the standard and most used:

AddDefaultCharset utf-8 
AddCharset utf-8 .html .css .js .xml .json .rss .atom

Make sure audio/video works by defining the MIME type on the server:

AddType video/ogg .ogv
AddType video/mp4 .mp4
AddType video/webm .webm

More here: http://perishablepress.com/stupid-htaccess-tricks/#redirects