Htaccess tricks
From DreamHost
Here are some useful tips, and tricks for things you can or may want to do with your Dream Host account using htaccess
Set Timezone
SetEnv TZ America/Chicago
This will allow PHP to out put your current time instead of the server's time.
List of Timezones: http://us2.php.net/manual/en/timezones.php
Different File Extension
Options +FollowSymlinks RewriteEngine On RewriteBase / RewriteRule ^(.+)\.zig$ /$1.php [NC,L]
In this example, this will allow you to refer to your files as example.zig instead of example.php
ForceType and PHP 5
This will force the use of PHP4:
ForceType application/x-httpd-php
This will force the use of PHP5:
ForceType php5-cgi
No File Extension
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /$1.php [L,QSA]
In this example, this will completely remove the file extension from your URL such as example instead of example.php
Google Text Translation
Options +FollowSymlinks RewriteEngine On RewriteBase / RewriteRule ^(.*)-(fr|de|es|it|pt)$ http://www.google.com/translate_c?hl=$2&sl=en&u=http://site.com/$1 [R,NC]
This will redirect any page on your site ending in -fr, -de, -es, etc. to the google.com translation for that language.
Force File Download
<FilesMatch "\.(mov|mp3|jpg|pdf)$"> ForceType application/octet-stream Header set Content-Disposition attachment </FilesMatch>
This example provides the client requesting a .mov or .mp3 file the ability to save the file directly instead of having the file open in the browser or with a third-party plugin or other software like QuickTime, Windows Media Player, iTunes, etc..
Note: The third line requires the mod_headers [1] that come with apache 2.0. That will not work with apache versions older than version 2.0.
Deny Access to Include Files
<Files ~ "\.inc$"> Order Allow,Deny Deny from All </Files>
Don't want people to see some sensitive information in your inc files? Use the above to display a 403 Forbidden error instead.
Deny Access Directory Listing
Options -Indexes
Don't have an index in all your directory's? Use this to deny access to all the directory listings, if there is no index file.
Fail-safe Directory Listing
Alternatively you can specify that a specific file be displayed when there is no default index setup.
Options -Indexes DirectoryIndex index.php index.html /index.php
When a visitor requests a directory Apache will search for index.php, then index.html, and if neither are found it will display /index.php
Error Documents
ErrorDocument 404 /errors/404.html ErrorDocument 403 /errors/403.html ErrorDocument 500 /errors/500.html
If you don't like Apache's default error pages, create your own, place them in the appropriate directory the example above puts them in a directory called errors which is located in the root of the current web site.
404 Redirect
ErrorDocument 404 http://exmple.com/
Instead of going to a 404 page, maybe you want to go to your homepage instead or some other page, just use the above
Faster Page Load Times / Bandwidth Saver
-could cause problems, this is advanced
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl|jpg|png|gif)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>
This basically checks to see if mod_czip.c is found and if it is it will compress the files for you so they are faster to send to the browser. This supposedly speeds up download times 35-40%, and then the file size should supposedly go down to 55-65%.
Site Maintenance
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteRule .* /maintenance.html [R=307,L]
Is your site going into maintenance? If so place this in your htaccess. When you are done with maintenance just comment it out with the # symbol. You will also need to make the page maintenance.html which will be displayed to visitors.
Time Dependant Rewrites
- If the hour is 16 (4 PM) Then deny all access
RewriteCond %{TIME_HOUR} ^16$
RewriteRule ^.*$ - [F,L]

