16. htaccess File Settings

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to share and learn 16. htaccess file setting skills.
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) $ http://domainname.com/img/hotlink_f_o.png1_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

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.