. htaccess file Setup Tips 16

Source: Internet
Author: User
Tags modify php and require valid domain domain name root directory

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.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 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. *. (gifjpgpng) $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 403http://www.youdomain.com
Order Deny,allow
Deny from all
Allow from 124.34.48.165
Allow from 102.54.68.123

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 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]



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.