Programmers do not know the 16. htaccess File Setup Tips

Source: Internet
Author: User
Reproduced original address: http://www.jb100.net/html/content-22-741-1.html

. 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.htmlhttp://www.htaccesselite.com/r/file.html

5. Set up e-mail for the server administrator.

Serversignature EMail
SETENV SERVER_ADMINdefault@domain.com

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) $[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 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, 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 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. 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]

Reproduced original address: http://www.jb100.net/html/content-22-741-1.html

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.