Here's my list of the ultimate htaccess code snippets and examples that I use all the time. I tried to keep them extremely minimalistic.
Don't miss checking out my example htaccess file, its very comprehensive
Hey jason I'm glad someone finally noticed the "development" htaccess at the bottom of my file! Thats honestly the piece of code I am most glad to have.
This lets google crawl the page, lets me access the whole site (24.205.23.222) without a password, and lets my client access the page WITH a password. It also allows for XHTML and CSS validation! (w3.org)
# ELITE HTACCESS FOR WEBDEVELOPERS##############################################AuthName "SiteName Administration"AuthUserFile /home/sitename.com/.htpasswdAuthType basicRequire valid-userOrder deny,allowDeny from allAllow from 24\.205\.23\.222Allow from w3.org htmlhelp.comAllow from googlebot.comSatisfy Any
Each code snippet has been copied from htaccesselite. Additional and detailed info on each htaccess code snippet can be found at askapache.comNOTE: Most of these snippets can be used with a Files or Filesmatch directive to only apply to certain files.NOTE: Any htaccess rewrite examples should always begin with:Options +FollowSymLinksRewriteEngine OnRewriteBase /
#Makes image.gif, blah.html, index.cgi all act as phpForceType application/x-httpd-php
Additional https/ssl information and Apache SSL in htaccess examples
SSLOptions +StrictRequireSSLRequireSSLSSLRequire %{HTTP_HOST} eq "google.com"ErrorDocument 403 https://google.com
Redirect 301 /d/file.html http://www.htaccesselite.com/r/file.html
RedirectMatch 301 /blog(.*) http://www.askapache.com/$1
Redirect 301 / http://www.newdomain.com
Options +FollowSymLinksRewriteEngine OnRewriteBase /RewriteCond %{REQUEST_URI} !^/robots\.txt$RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Options +FollowSymLinksRewriteEngine OnRewriteBase /RewriteCond %{REQUEST_URI} !^/robots\.txt$ [NC]RewriteCond %{HTTP_HOST} !^www\.[a-z-]+\.[a-z]{2,6} [NC]RewriteCond %{HTTP_HOST} ([a-z-]+\.[a-z]{2,6})$ [NC]RewriteRule ^/(.*)$ http://%1/$1 [R=301,L]
Options +FollowSymLinksRewriteEngine OnRewriteBase /RewriteCond %{REQUEST_URI} !^/robots\.txt$RewriteCond %{HTTP_HOST} \.([a-z-]+\.[a-z]{2,6})$ [NC]RewriteRule ^/(.*)$ http://%1/$1 [R=301,L]
Options +FollowSymLinksRewriteEngine OnRewriteBase /RewriteCond %{HTTP_HOST} \.([^\.]+\.[^\.0-9]+)$RewriteCond %{REQUEST_URI} !^/robots\.txt$ [NC]RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
ErrorDocument 403 http://www.someothersite.comOrder deny,allowDeny from allAllow from 24.33.65.6
SetEnv TZ America/Indianapolis
SetEnv SERVER_ADMIN webmaste@htaccesselite.com
ServerSignature Off
Article: Setting Charset in htaccess
Article: Using FilesMatch and Files in htaccess
AddDefaultCharset UTF-8# Or AddType 'text/html; charset=UTF-8' htmlDefaultLanguage en-US
AddDefaultCharset UTF-8 DefaultLanguage en-US
AddDefaultCharset UTF-8 DefaultLanguage en-US
Article: Custom PHP.ini tips and tricks
When php run as Apache Module (mod_php)in root .htaccessSetEnv PHPRC /location/todir/containing/phpinifileWhen php run as CGIPlace your php.ini file in the dir of your cgi’d php, in this case /cgi-bin/htaccess might look something like this
AddHandler php-cgi .php .htm Action php-cgi /cgi-bin/php5.cgiWhen cgi’d php is run with wrapper (for FastCGI)You will have a shell wrapper script something like this:#!/bin/sh
export PHP_FCGI_CHILDREN=3 exec /user3/x.com/htdocs/cgi-bin/php5.cgiChange To
#!/bin/sh
export PHP_FCGI_CHILDREN=3 exec /x.com/cgi-bin/php.cgi -c /abs/path/to/php.ini
AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi Options -ExecCGI
This is cool, you are basically categorizing all those files that end in those extensions so that they fall under the jurisdiction of the -ExecCGI command, which also means -FollowSymLinks (and the opposite is also true, +ExecCGI also turns on +FollowSymLinks)Options -ExecCGI -Indexes -All +FollowSymLinksRewriteEngine OnRewriteBase /RewriteCond %{REQUEST_METHOD} !^(GET PUT)RewriteRule .* - [F]
Action image/gif /cgi-bin/filter.cgi
Script PUT /cgi-bin/upload.cgi
AddType application/octet-stream .avi AddType application/octet-stream .mpg
Then in your HTML you could just link directly to the file..
Download Movie1
And then you will get a pop-up box asking whether you want to save the file or open it.
If you'd rather have .pl, .py, or .cgi files displayed in the browser as source rather than be executed as scripts, simply create a .htaccess file in the relevant directory with the following:
RemoveHandler cgi-script .pl .py .cgi
Article: Speed Up Sites with htaccess Caching
# MONTH Header set Cache-Control "max-age=2592000" # WEEK
Header set Cache-Control "max-age=604800" # DAY
Header set Cache-Control "max-age=43200"
Options +FollowSymLinksRewriteEngine OnRewriteBase /RewriteCond %{HTTP_REFERER} !^$RewriteCond %{HTTP_REFERER} !^http://(www\.)?askapache.com/.*$ [NC]RewriteRule \.(gif jpg swf flv png)$ http://www.askapache.com/feed.gif [R=302,L]
Article: Additional ErrorDocument Info and Examples
ErrorDocument 404 /favicon.icoErrorDocument 403 https://secure.htaccesselite.comErrorDocument 404 /cgi-bin/error.phpErrorDocument 400 /cgi-bin/error.phpErrorDocument 401 /cgi-bin/error.phpErrorDocument 403 /cgi-bin/error.phpErrorDocument 405 /cgi-bin/error.phpErrorDocument 406 /cgi-bin/error.phpErrorDocument 409 /cgi-bin/error.phpErrorDocument 413 /cgi-bin/error.phpErrorDocument 414 /cgi-bin/error.phpErrorDocument 500 /cgi-bin/error.phpErrorDocument 501 /cgi-bin/error.php
Note: You can also do an external link, but don't do an external link to your site or you will cause a loop that will hurt your SEO.
AuthName "Prompt" AuthType Basic AuthUserFile /home/askapache.com/.htpasswd Require valid-user
AuthName "Development" AuthUserFile /.htpasswd AuthType basic Require valid-user
# A (partial) domain-nameAllow from 10.1.0.0/255.255.0.0 # Full IP addressAllow from 10.1.2.3 # More than 1 full IP addressAllow from 192.168.1.104 192.168.1.205 # Partial IP addresses # first 1 to 3 bytes of IP, for subnet restriction.Allow from 10.1Allow from 10 172.20 192.168.2 # network/netmask pairAllow from 10.1.0.0/255.255.0.0 # network/nnn CIDR specificationAllow from 10.1.0.0/16 # IPv6 addresses and subnetsAllow from 2001:db8::a00:20ff:fea7:cceaAllow from 2001:db8::a00:20ff:fea7:ccea/10
Article: Additional SetEnvIf examples
SetEnvIf User-Agent ^KnockKnock/2\.0 let_me_inOrder Deny,AllowDeny from allAllow from env=let_me_in
Order Allow,DenyAllow from apache.orgDeny from foo.apache.org
AuthUserFile /home/www/site1-passwdAuthType BasicAuthName MySiteRequire valid-userAllow from 172.17.10Satisfy Any
Options +FollowSymLinksRewriteEngine OnRewriteBase /# If the hour is 16 (4 PM) Then deny all accessRewriteCond %{TIME_HOUR} ^16$RewriteRule ^.*$ - [F,L]
I use this when I start a new site, and uncomment or delete parts of the file depending on the sites needs
# DEFAULT SETTINGS##############################################Options +ExecCGI -IndexesDirectoryIndex index.php index.html index.htm### DEFAULTS ###
ServerSignature OffAddType video/x-flv .flvAddType application/x-shockwave-flash .swfAddType image/x-icon .icoAddDefaultCharset UTF-8DefaultLanguage en-USSetEnv TZ America/IndianapolisSetEnv SERVER_ADMIN webmaster@askapache.com### FAST-CGI ###
AddHandler fastcgi-script fcgiAddHandler php-cgi .phpAction php-cgi /cgi-bin/php5-wrapper.fcgi# HEADERS and CACHING
################################################## CACHING ##### YEAR Header set Cache-Control "max-age=2592000" # WEEK Header set Cache-Control "max-age=604800" # 10 minutes Header set Cache-Control "max-age=600" # DONT CACHE Header unset Cache-Control # REWRITES AND REDIRECTS
################################################# SEO REDIRECTS ###Redirect 301 /2006/uncategorized/htaccesselitecom-aboutus.html http://www.^^SITE^^.^^TLD^^### REWRITES ###
RewriteEngine OnRewriteBase /### WORDPRESS ###
# BEGIN WordPressRewriteEngine OnRewriteBase /RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L] # END WordPress
# AUTHENTICATION
##############################################AuthName "askapache.com"Require valid-userAuthUserFile /askapache/.htpasswdAuthType basic