The. htaccess file (hypertext Access file) is a very powerful configuration file for the Apache Web server, and for this file, Apache has a bunch of parameters that allow you to configure almost arbitrary functionality ... htaccess The configuration file insists on a culture of Unix-using a plain text file in ASCII to configure access policies for your site.
This article includes 16 very useful tips. In addition, 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 when the problem occurs, it is easy to recover.
1. Use. htaccess to create a custom error page. This is a very simple thing for Linux Apache. Use the following. htaccess syntax you can easily complete this function. (Put the. htaccess in your site root directory)
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 for your Web site
Setenv TZ America/houston
3. Block IP List
There are times when you need to block some access in the form of an IP address. Whether for an IP address or a network segment, this is a very simple thing, as follows:
Allow from all
Deny from 145.186.14.122
Deny from 124.15
Apache returns a 403 error for the rejected IP.
4. Transfer some old links to the new links-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 default@domain.com
6. Use. htaccess visit to stop hotlinking. If a picture on your site is referenced by another n-more site, it is likely to degrade your server's performance, using the following code to protect some popular links from being too much referenced.
Options +followsymlinks
# Protect Hotlinking
Rewriteengine on
Rewritecond%{http_referer}!^$
Rewritecond%{http_referer}!^http://(www.)? domainname.com/[NC]
Rewriterule. *. (gif|jpg|png) $ yun_qi_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 specific 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 the file directly instead of downloading-usually, when we open the online files, there will always be a dialog box to ask us to download or directly open, use the following settings will not appear this problem, directly open .
AddType Application/octet-stream. pdf
AddType application/octet-stream. zip
AddType Application/octet-stream. mov
10. Modify file Type – The following example allows any file to become PHP and is interpreted by the server. 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]