Reproduced original address: http://www.jb100.net/html/content-22-741-1.html
The. htaccess file is a very powerful configuration file of the Apache Web server. for this file, Apache has a bunch of parameters that allow you to configure almost anything you want .. The htaccess configuration file adheres to a Unix culture-using an ASCII plain text file to configure your website access policy.
This article includes 16 useful tips. In addition, because. htaccess is a very powerful configuration file. Therefore, a slight syntax error may cause your entire website to fail. Therefore, when you modify or replace the original file, make sure to back up the old files so that they can be easily restored in case of problems.
1. use. htaccess to create a custom error page. This is an extremely simple task for Linux Apache. You can use the following. htaccess syntax to easily complete this function. (Put. 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 website
SetEnv TZ America/Houston
3. blocked IP list Sometimes, you need to block some access by IP address. Whether it is an IP address or a network segment, this is a very simple task, as shown below:
Allow from all Deny from 145.186.14.122 Deny from 124.15
Apache returns the 403 error for the rejected IP address.
4. transfer some old links to new links-search engine optimization SEO
Redirect 301/d/file.html http://www.htaccesselite.com/r/file.html
5. set email for the server administrator.
ServerSignature EMail SetEnv SERVER_ADMINdefault@domain.com
6. use. htaccess to access and Stop leeching. If an image on your website is referenced by more than N other websites, this may cause your server performance to decline, use the following code to protect some popular links from being referenced too much.
Options + FollowSymlinks # Protect Hotlinking RewriteEngine On RewriteCond % {HTTP_REFERER }! ^ $ RewriteCond % {HTTP_REFERER }! ^ Http: // (www .)? Domainname.com/javasnc] RewriteRule. *. (gif | jpg | 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 from some special IP addresses to other sites
ErrorDocument 403 http://www.youdomain.com Order deny, allow Deny from all Allow from 124.34.48.165 Allow from 102.54.68.123
9. directly open the file instead of downloading it. Normally, when opening the online file, a dialog box is always displayed asking whether to download or directly open the file. this problem will not occur if you use the following settings, open directly.
AddType application/octet-stream. pdf AddType application/octet-stream. zip AddType application/octet-stream. mov
10. modify the file type. in the following example, any file can be interpreted by the server as PHP. For example, myphp, cgi, and phtml.
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 fileorder allow, deny Deny from all 13. blocking directory browsing
# Disable directory browsing Options All-Indexes
14. set the default homepage
# Serve alternate default index page DirectoryIndex about.html
15. Password Authentication-you can create a file for authentication. The following 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. convert old domain names to new ones
# Redirect from old domain to new domain RewriteEngine On RewriteRule ^ (. *) $ http://www.yourdomain.com/%1%r%301,l]
Reproduced original address: http://www.jb100.net/html/content-22-741-1.html
|