. Htaccess requires apache to enable VM support. htaccess can implement the following functions. Next I will introduce it. htaccess blocks access from some ip addresses, redirects and url rewriting, limits access to special resources, and implements caching and other functions.
What is htaccess?
In summary, the htaccess file is a configuration file on the Apache server, which is responsible for the web page configuration under the relevant directory. The htaccess file can help us achieve: webpage 301 redirection, custom 404 error pages, changing file extensions, allowing/blocking access to specific users or directories, forbidding directory lists, and configuring default documents.
A simple redirection
The Code is as follows: |
Copy code |
Redirect 301 export old.html $ http: // localhost/new.html |
Set an httpstatus code to 301( )) newnewnewnew.html. The regular expression is used to match the accessed page. The url following the regular expression must be the complete path.
Url rewriting
The Code is as follows: |
Copy code |
RewriteEngine on RewriteRule implements old.html $ new.html |
The first line opens the rewrite Engine
The second line is to rewrite the rule. In this case, when using old.html, new.html is used, and the user knows this well. The old display in the address bar is old.html.
To display a new access address in the address bar, we can add an f sign to the rewriterule statement.
The Code is as follows: |
Copy code |
RewriteRule into old.html $ http: // hostname/new.html [r = 301] The following describes how to rewrite a url with query parameters to a search engine-friendly url. RewriteRule ^ products/([^/] +)/([^/] +)/([^/] +) product. php? Cat = $1 & brand = $2 & prod = $3 |
This rule allows visitors
The Code is as follows: |
Copy code |
Url products/turntables/TechCrunch/sl1210, |
To access product. php? Cat = turntables & brand = techbench & prod = sl1210.
It is a unit in parentheses, corresponding to $1, $2, and $3 respectively. [^/] + Represents one or more characters except/in the original brackets.
Custom error page
The Code is as follows: |
Copy code |
ErrorDocument 404 "/404.html" |
This is an error that cannot be found on the 404 page, or a page that defines other error codes.
Restrict access to special resources
The Code is as follows: |
Copy code |
<Files "protectedfile.html"> AuthName "Username and password required" AuthUserFile/path/to/. htpasswd Require valid-user AuthType Basic </Files> |
Protect protectedfile.html, authName is the prompt information in the login box, And authuserfile is the path of the. htpasswd File
Block access from some ip addresses
The Code is as follows: |
Copy code |
Order allow, deny Deny from 192.168.0.1 Allow from allOrder |
Tells the apache command order. This is allowed first and then rejected. Although allow from all appears later, it still takes effect later. Access to 192.168.0.1 is denied. You can also block access by segment, as long as you specify only the first few segments, it can be similar, 192.168.
Implement Cache
The Code is as follows: |
Copy code |
ExpiresActive on ExpiresByType image/gif "access plus 1 month" ExpiresByType image/png "access plus 1 month" ExpiresByType image/jpg "access plus 1 month" ExpiresByType image/jpeg "access plus 1 month" |
The first line is enabled. The following controls the cache period of each resource plus one month. This function depends on the mod_expires module of apache.
Authentication example
If you only want to know How to authenticate the password, you can directly start from here. It is important to note that there is a common misunderstanding that password authentication must be used. the htaccess file is actually incorrect. It is better to put the authentication command in the <Directory> segment of the main configuration file, and the. htaccess file should only be used when you do not have access to the main configuration file. Refer to the above discussion on when and when the. htaccess file should not be used.
If you still need to use the. htaccess file, continue with the instructions below.
. Htaccess file content:
The Code is as follows: |
Copy code |
AuthType Basic AuthName "Password Required" AuthUserFile/www/passwords/password. file AuthGroupFile/www/passwords/group. file Require Group admins |
AllowOverride AuthConfig must be set to allow these commands to take effect.
Server inclusion (SSI) Example
Another common purpose of the. htaccess file is to allow a specific directory to use server-side inclusion (SSI). You can place the. htaccess file in the desired directory and make the following Configuration:
The Code is as follows: |
Copy code |
Options + des AddType text/html shtml AddHandler server-parsed shtml |
Note: AllowOverride Options and AllowOverride FileInfo must be set at the same time for these commands to take effect.
The scope of the directive. the configuration commands in the htaccess file act on. the directory where the htaccess file is located and all its subdirectories. However, it is important to note that its parent directory may also exist. the htaccess file, and the commands take effect in order of search, so the command under a specific directory. commands in the htaccess file may overwrite the commands in the parent directory. commands in the htaccess file, that is, the commands in the subdirectory overwrite the commands in the parent directory or main configuration file.
Example:
The Code is as follows: |
Copy code |
/Www/htdocs/example1 |
Directory. htaccess
The file contains the following content:
Options + ExecCGI
(Note: You must set "AllowOverride Options" to allow the "Options" command to be used in. htaccess)
The. htaccess file in the/www/htdocs/example1/example2 directory contains the following content:
Options shortdes