. Htaccess basic syntax and Application
. Htaccess is a very powerful distributed configuration file of the Apache server.
Correct understanding and use of the. htaccess file can help us optimize our servers or virtual hosts.
How to enable htaccess
Take windows as an example. Go to the Apache/conf directory, find the httpd. conf file, and remove it.
Loadmodule rewrite_module modules/mod_rewrite.so
First #, set the Directory attribute AllowOverride all, and restart Apache.
Common formats
The following is a typical htaccess file.
# Enable URL rewriting rewriteengine on
# URL rewriting scope rewritebase/path/to/URL
# What conditions are met: rewritecond % {http_host }! ^ Www \. Example \. com $ [Nc]
# What kind of rule rewriterule is applied .? Http://www.example.com % {request_uri} [R = 301, l]
Let's take a look at rewritecond. First, there is a %, because {http_host} is an Apache variable and needs to be indicated by %. From! The start is the matching condition. Regular Expressions are supported .! Not equal to: If http_host is not www.example.com. The following [Nc] (no case) indicates that case sensitivity is ignored.
- [L] (last): terminates a series of rewritecond and rewriterule
- [R] (redirect): triggers a displayed jump. You can also specify the jump type, for example, [R = 301].
- [F] (Forbidden): disable viewing specific files. Apache will trigger the 403 error.
Typical applications
Image anti-leech
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 normal image leeching, because http_referer is more prone to forgery.
Custom Error 404 page
If you enter a URL that does not exist, the custom error page is displayed.
Errordocument 404/404 .html # others likewise errordocument 500/500 .html
Process moved files
Redirect 301/old.html http://yoursite.com/new.html
# It can also be the following rewriterule/old.html http://yoursite.com/new.html [R = 301, l]
# If you want to implicitly jump (the URL address remains unchanged, but the content is actually another URL), use the following rewriterule/old.html http://yoursite.com/new.html [l]
There are many more rewriteruleArticleCan be done, such
# Link an HTML suffix URL to a PHP File
#$1 refers to the rewriterule ^ /? ([A-z/] +) \. html $ 1.php [l]
# Or link the content of the old folder to the new folder rewriterule ^ /? Old_directory/([A-z/.] +) $ new_directory/$1 [R = 301, l]
# Hide the file name rewriterule ^ /? ([A-Z] +) $ 1.php [l]
Disable directory list display
If there is no index file in the directory and no special processing is performed on the directory, especially on Windows hosts, the contents in the directory will be displayed, in this case, you can create. htaccess file, and then write
Options-indexes # That's all you need.
Block/allow specific IP addresses/IP segments
# Disable all IP addresses except the specified order deny and allowdeny 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 <files page.html> allow from all </Files>
# If you want to disable a specific ipdeny from 123.123.123.123
Add MIME type
Addtype video/X-FLV. FLV
# If the setting type is application/octet-stream, the system will prompt you to download addtype application/octet-stream. pdf.