Apache Tips and Tricks

Setting a default character set

One of the biggest things people overlook when setting up utf-8 pages is the default character set that is sent vis http headers. It has been shown to be a performance boost to boot.

My favorite way is setting the default character set in the .htaccess file. It's simple!

To just enable it for latin 1 (ISO-8859-1), you just do:

AddDefaultCharset on

For utf-8:

AddDefaultCharset utf-8

and that's it!

mod_rewrite a domain to www.domain

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

mod_rewrite a subdomain to a subdirectory

RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+\..+)$
RewriteCond %{HTTP_HOST} !^www\.                       [NC]
RewriteCond %{DOCUMENT_ROOT}/%1 -d                     [OR]
RewriteCond %{DOCUMENT_ROOT}/%1 -f
RewriteRule ^(.*)$ http://www.%2/%1/$1                 [L,R=301]
        

mod_rewrite rules for subdomain remapping

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

mod_rewrite rules to force SSL or disable SSL

# Rewrite https to http
 RewriteCond %{HTTPS} =on
 RewriteRule ^(.*)$ http://%{SERVER_NAME}/$1 [L,R=301]
# Rewrite https to http
 RewriteCond %{HTTPS} =off
 RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [L,R=301]