. htaccess files (hypertext Access file) is a very powerful configuration file for Apache Web servers, and for this file, Apache has a bunch of parameters that allow you to configure almost any function you want. htaccess Configuration files adhere to a Unix culture-use an ASCII plain text file to configure your site's access policy.
This article contains 16 very useful tips. Also, because. htaccess is a fairly powerful configuration file, so a slight grammatical error can cause your entire site to fail, so when you modify or replace the original file, be sure to back up the old file, so that the problem is easy to recover.
1. Create a custom error page using the. htaccess. This is a very simple thing for Linux Apache. Use the following. htaccess syntax you can easily complete this function. (Put the. htaccess in the root directory of your website)
ErrorDocument 401/error/401.php
ErrorDocument 403/error/403.php
ErrorDocument 404/error/404.php
ErrorDocument 500/error/500.php
2. Set the time zone of the site
SETENV TZ America/houston
3. Block IP List
There are times when you need to block some access with an IP address. This is a very simple thing, whether for an IP address or a network segment, as follows:
Allow from all
Deny from 145.186.14.122
Deny from 124.15
Apache will return a 403 error for rejected IPs.
4. Transfer some old links to the new link-search engine optimization seo
Redirect 301/d/file.html http://www.htaccesselite.com/r/file.html
5. Set up e-mail for the server administrator.
Serversignature EMail
SETENV server_admin [email protected]
6. Use. htaccess to visit hotlinking. If a picture on your site is referenced by another n-more site, then this is likely to cause your server's performance to degrade, using the code below to protect certain popular links from excessive references.
Options +followsymlinks
# Protect Hotlinking
Rewriteengine on
Rewritecond%{http_referer}!^$
Rewritecond%{http_referer}!^http://(www.) domainname.com/[NC]
Rewriterule. *. (gif|jpg|png) $ http://domainname.com/img/hotlink_f_o.png [NC]
7. Block all requests from the User Agent
# #. htaccess Code:: BEGIN
# # Block Bad Bots by user-agent
Setenvifnocase user-agent ^frontpage [Nc,or]
Setenvifnocase user-agent ^java.* [Nc,or]
Setenvifnocase user-agent ^microsoft.url [Nc,or]
Setenvifnocase user-agent ^msfrontpage [Nc,or]
Setenvifnocase user-agent ^offline.explorer [Nc,or]
Setenvifnocase user-agent ^[ww]eb[bb]andit [Nc,or]
Setenvifnocase user-agent ^zeus [NC]
Order Allow,deny
Allow from all
Deny from Env=bad_bot
# #. htaccess Code:: END
8. Redirect Requests for certain special IP addresses to other sites
ErrorDocument 403 http://www.youdomain.com
Order Deny,allow
Deny from all
Allow from IP
Allow from IP
9. Find files directly instead of downloading – usually, when we open an online file, we always get a dialog box asking if we are downloading or opening it directly, and using the settings below will not have this problem, just open it.
AddType Application/octet-stream. pdf
AddType application/octet-stream. zip
AddType Application/octet-stream. mov
10. Modify the file type – The following example allows any file to be interpreted by the server as PHP. For example: myphp, cgi,phtml and so on.
Forcetype application/x-httpd-php
SetHandler application/x-httpd-php
11. Block access to. htaccess files
# Secure Htaccess file
Order Allow,deny
Deny from all
12. Protect files on the server from being accessed
# Prevent access of a certain file order Allow,deny
Deny from all
13. Block Directory Browsing
# Disable Directory browsing
Options all-indexes
14. Set the default home page
# Serve alternate default index page
DirectoryIndex about.html
15. Password Authentication – You can create a file for authentication. Here is an example:
# to protect a file
AuthType Basic
AuthName "Prompt"
authuserfile/home/path/.htpasswd
Require Valid-user
# Password-protect a directory
Resides
AuthType Basic
AuthName "This directory is protected"
authuserfile/home/path/.htpasswd
Authgroupfile/dev/null
Require Valid-user
16. Turn the old domain name into a new domain name
# REDIRECT from old domain to new domain
Rewriteengine on
Rewriterule ^ (. *) $ http://www.yourdomain.com/$1 [r=301,l]
Source: http://rafeekphp.wordpress.com/2009/06/06/16-great-htaccess-tricks-and-hacks/
16 simple and practical. Htaccess Tips