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_ADMIN default@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/[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 from some 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. 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 file order 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]
17. Do not open the downloaded file directly.
<Files *. *>
ForceType application/octet-stream
</Files>