. htaccess Redirection and URL rewriting

Source: Internet
Author: User
Tags parent directory regular expression

What is htaccess

As an overview, the htaccess file is a configuration file in the Apache server that is responsible for the page configuration in the related directory. Through the htaccess file, you can help us achieve: Web 301 redirect, custom 404 error page, change file extension, allow/block specific user or directory access, prohibit directory list, configure default document and other features.


A simple redirect

The code is as follows Copy Code
Redirect ^old.html$ http://localhost/new.html

This is to set an HTTP status code of 301 (permanently moved) and turn all Access old.html to new.html. A regular expression is used to match the visited page, and the following URL must be the full path.

URL Rewrite

The code is as follows Copy Code

Rewriteengine on
Rewriterule ^old.html$ new.html

The first line opens the override engine
The second line is the overriding rule. This allows the user to access the old.html is actually a visit to the new.html, and users of this well informed. Because the address bar still shows old.html.

In order to display the new access address in the address bar, we can add an F sign in the Rewriterule statement.

The code is as follows Copy Code

Rewriterule ^old.html$ http://hostname/new.html [r=301] The following is the URL with the query parameter rewritten to the search engine friendly URL

Rewriterule ^products/([^/]+)/([^/]+)/([^/]+) product.php?cat=$1&brand=$2&prod=$3

This rule allows visitors to do something like this

The code is as follows Copy Code
URL products/turntables/technics/sl1210,

To access product.php?cat=turntables&brand=technics&prod=sl1210.

A unit in parentheses corresponding to the $1,$2,$3 of the rear. [^/]+ any 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 the definition of an error that 404 pages cannot find, or a page that can define other error codes.

Restricting access to feature 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>

Protection Protectedfile.html,authname is the message of the landing box, AuthUserFile is the path to the. htpasswd file

Block certain IP access

The code is as follows Copy Code

Order Allow,deny
Deny from 192.168.0.1
Allow from Allorder

Tells the order of Apache directives. This one here is first allowed and then rejected. Although allow from all appears, it still takes effect before. Denied 192.168.0.1 's visit. You can also block access by paragraph, as long as you specify only the preceding paragraphs to be similar, 192.168.

Implementing caching

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 open, and the following controls each resource with a one-month cache period. This feature relies on Apache's mod_expires modules.


Certification examples
If you just want to know how to authenticate, and directly from here to start to see, there is a very important point to note, there is a common misconception that the implementation of password authentication must use the. htaccess file, in fact, is not true. It is a better way to place the authentication instructions in the <Directory> section of the main configuration file, and the. htaccess file should only be used when the primary configuration file is not authorized. See the above discussion about when and when you should not use the. htaccess file.
If you still need to use the. htaccess file, please continue with the following instructions.

The contents of the. htaccess file:

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 instructions to take effect.
Server-side include (SSI) examples
Another common use of. htaccess files is to allow a specific directory to use server-side inclusion (SSI), where you can place the. htaccess file in the directory you want, and configure it as follows:

The code is as follows Copy Code

Options +includes
AddType text/html shtml
AddHandler server-parsed shtml

Note that both the allowoverride Options and the allowoverride FileInfo must be set up for these instructions to take effect.


The scope of the directive. The configuration directives in the Htaccess file act on the directory where the. htaccess file resides and all of its subdirectories, but it is important to note that the Its parent directory may also be available. htaccess files, and the instructions are in the order of lookup, the instructions in the. htaccess file in a particular directory might overwrite the instructions in the. htaccess file in their parent directory, that is, the instructions in the subdirectory will overwrite the instructions in the parents directory or in the master configuration file.

Example:

The code is as follows Copy Code
/www/htdocs/example1

The. htaccess in the table of contents

The file has the following contents:
Options +execcgi

(Note: "allowoverride options" must be set to allow the "options" command to be used in. htaccess)
The. htaccess file in the/www/htdocs/example1/example2 directory has the following contents:
Options Includes

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.