The .htaccess file is a very powerful configuration file for the Apache Web server. With this file, Apache has a bunch of parameters that let you configure almost any functionality you want. The .htaccess config file sticks to a Unix culture - using an ASCII plain text file to configure your site's access policy.
This article includes 16 very useful tips. In addition, because .htaccess is a fairly powerful configuration file, a slight syntax error can cause a breakdown of your entire site, so be sure to back up the old files as you make changes or replace them so that When problems arise can be easily restored.
Use .htaccess to create a custom error page. This is an extremely simple task for Linux Apache. Use the following .htaccess syntax you can easily accomplish this function. (Put .htaccess on the root 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 website's time zone
SetEnv TZ America / Houston
3. Block IP list Sometimes, you need to block some access by IP address. Whether for an IP address or a network segment, this is a very simple matter, as follows:
allow from all
deny from 145.186.14.122
deny from 124.15
Apache will return a 403 error for the rejected IP.
4. Transfer old links to new links - Search Engine Optimization SEO
Redirect 301 /d/file.htmlhttp://www.htaccesselite.com/r/file.html
5. Set up email for the server administrator.
ServerSignature EMail
SetEnv SERVER_ADMINdefault@domain.com
6. Use. Htaccess to visit the hotlinking. If a picture on your site is referenced by another N more sites, then this is likely to lead to a decrease in your server's performance. Use the following code to protect some popular links from being overused.
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 User Agent requests
## .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. The request of some special IP address is redirected to other sites
ErrorDocument 403http: //www.youdomain.com
Order deny, allow
Deny from all
Allow from 124.34.48.165
Allow from 102.54.68.123
9. Find files directly instead of downloading - Usually, when we open an online file there will always be a dialog box asking us to download or directly open, use the following settings will not appear this problem, open directly.
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 the file on the server to be accessed
# prevent access of a certain fileorder 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. The old domain name to a new domain name
# redirect from old domain to new domain
RewriteEngine On
RewriteRule ^ (. *) $ Http: //www.yourdomain.com/$1 [R = 301, L]