Image may be NSFW.
Clik here to view..htaccess (hypertext access) is the default name of a directory-level configuration file that allows for decentralized management of web server configuration.
There are tons of things you can do from the htaccess file inside of your web hosting account. From redirecting the users to controlling who can get in and who can get out, it is a very useful file to have on your hosting account. To help out the new web hosting clients and to act as a reminder to the old pros around here, I thought I would go through five of the most popular .htaccess tips I know.
Redirect to a Maintenance Page
This is a great one to use if you need to make some quick web page edits, and you don’t want the public to see you edit your web site, while it is still live.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/offline.html$
RewriteRule .* /offline.html [R=307,L]
This will redirect anybody who checks out any web page on your web site to the “offline.html” file.
Force the Use of “www” in Front of a Domain Name
If you want to keep the www in front of your domain name, no matter how somebody types it in, here is the ‘bit of .htaccess magic you will need:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
Once you have done so, both mydomain.com and www.mydomain.com will both send you to www.mydomain.com
Remove the “www” From in Front of a Domain Name
You can also force your domain to drop the www from the address when typed in, if you would rather have it the other way around. In that situation, you would need to add this to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [R=301,L]
This will make both mydomain.com and www.mydomain.com send you to just mydomain.com.
Manually Create Custom Error Pages
Ever check out some of those customized error pages and wonder how it could be done? Just use these snippets of .htaccess code to tell people where to look for your error pages.
ErrorDocument 400 /errors/400.html
ErrorDocument 401 /errors/401.html
ErrorDocument 403 /errors/403.html
ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.html
This tells the server to look inside the “errors” folder and that specific page for each of the different error types. That is not all of the error codes, but they are the ones that will be ‘hit by your users 99 percent of the time.
Block Visitor By IP Address
Need to keep somebody from checking out your web site? This next tip will give you the ability to deny somebody based on what their IP address is:
order allow,deny
deny from 123.45.6.7
deny from 012.34.5.
allow from all
The above blocks access to the site from 123.45.6.7, and from any sub-domain under the IP block 012.34.5. (012.34.5.1, 012.34.5.2, 012.34.5.3, etc).
Before you go off and plant htaccess everywhere, read through this and make sure you don’t do anything redundant, since it is possible to cause an infinite loop of redirects or errors if you place something weird in the htaccess.
Also…some sites do not allow use of htaccess files, since depending on what they are doing, they can slow down a server overloaded with domains if they are all using htaccess files. I can’t stress this enough: You need to make sure you are allowed to use htaccess before you actually use it. Some things that htaccess can do can compromise a server configuration that has been specifically setup by the admin, so don’t get in trouble.