Htaccess Basic Grammar and application(2012-11-09 16:13:47)
reproduced
| tags: htaccess it |
network |
. htaccess is a very powerful distributed configuration file for Apache servers. the correct understanding and use of. htaccess files can help us optimize our own servers or virtual hosts.
How to enable htaccess take windows as an example, go to the apache/conf directory, find the httpd.conf file, remove LoadModule rewrite_module modules/mod_ Rewrite.so in front of #, then set directory properties allowoverride all, restart Apache
Common formats
The following is a typical htaccess file
# turn on URL rewriting
Rewriteengine on
# Scope of URL overrides
# Rewritebase/path/to/url
# What conditions to meet
Rewritecond%{http_host}!^www\.example\.com$ [NC]
# What rules to apply
Rewriterule.? Http://www.example.com%{request_uri} [R=301,l]
Take a look at Rewritecond, first there is a%, because {http_host} is an Apache variable that needs to be indicated by%. Starting from! is the matching condition, which supports the regular. The meaning is not equal, this sentence means: If http_host is not www.example.com. The following [NC] (no case) indicates ignoring casing, and common
- L (last): Terminates a series of Rewritecond and Rewriterule
- R (redirect): Triggers a displayed jump, or you can specify a jump type, such as [r=301]
- F (Forbidden): Disable viewing of specific files, Apache will trigger 403 error
Typical Applications
Picture anti-theft chain
Rewritecond%{http_referer}!^$
Rewritecond%{http_referer}!^http://(www\.)? example\.com/[NC]
rewriterule \. (gif|jpg|png) $-[F]
Because it is based on http_referer verification, it can only prevent the general picture hotlinking, because Http_referer is relatively easy to forge
Customizing the 404 error page
If the user enters a URL that does not exist, the custom error page is displayed
errordocument 404/404.html
# Other Empathy
errordocument 500/500.html
Working with moved Files
Redirect 301/old.html http://yoursite.com/new.html
# It could be the following
rewriterule/old.html http://yoursite.com/new.html [r=301,l]
# If you want to have an implicit jump (the URL address is the same, but the content is actually a different URL), use the following
rewriterule/old.html http://yoursite.com/new.html [L]
There are a lot of articles that can be done for rewriterule, such as
# link HTML suffix URL to php file
# is referring to the previous 1th bracketed content
rewriterule ^/? ([a-z/]+) \.html$ $1.php [L]
# or link the contents of the old folder to the new folder
rewriterule ^/?old_directory/([a-z/.] +) $ new_directory/$1 [r=301,l]
# Hide file names
rewriterule ^/? ([a-z]+) $ $1.php [L] Suppress directory listing
If there is no index file in the directory, and the directory has not been specifically processed, especially the Windows host, then the contents of the directory will be displayed, you can create a. htaccess file in the root directory, and then write
options-indexes
# That's the way it's done
Block/allow specific IP/IP segments
# All IPs are forbidden except for the specified
Order Deny,allow
deny from all
# If you want to allow IP segments, such as 123.123.123.0 ~ 123.123.123.255, then
# Allow from 123.123.123.
Allow from 123.123.123.123
errordocument 403/page.html
allow from all
#如果想禁止特定IP
deny from 123.123.123.123
Add MIME type
addtype video/x-flv. flv
# If the setting type is Application/octet-stream will prompt to download
AddType application/octet-stream. pdf
Source: http://blog.sina.com.cn/s/blog_6e8b46e701014drc.html
Null
. htaccess basic syntax and application? (2012-11-09 16:13:47) reprinted