. htaccess tips: URL rewriting (Rewrite) and redirection (Redirect)

Source: Internet
Author: User
Tags regular expression zip


Catalog Table of Contents First, ready to start: Mod_rewrite. htaccess implements URL rewriting (rewrite) and URL redirection (redirect) to map an. htm page to a. PHP Temporary redirect ( r=302) and permanent redirection (r=301) Why use redirection. --the difference between redirection and URL rewriting long address translation remove www plus www support multi-domain access three, rewrite query string query_string use QSA to convert query string query_string use Rewritecond rewrite query string query_ String QSA and Rewritecond double Sword split query string four, using Rewritecond and Rewriterule access control file access control. htaccess block user-agent with. htaccess Block Hotlinking ( hot-linking) References

URL redirection is the. htaccess play, which can convert a long address to a short address, a dynamic address to a static address, redirect lost pages, prevent hotlinking, implement automatic language conversion, and so on. I think the difficulty is in the use of regular expressions and understanding. For a regular expression usage of htaccess, please refer to the article ". htaccess regular expression" on this site. First , ready to start: Mod_rewrite

The module that implements all these magical functions is called mod_rewrite, make sure that the module is installed and enabled on your server:

sudo a2enmod rewrite

We typically place all code that involves URL rewriting or redirection:

<ifmodule mod_rewrite.c>
 # Turn on rewrite engine
 Options +followsymlinks
 rewriteengine on
 # More rules below ...

Some of the things we need to be aware of: FollowSymLinks must be enabled, which is the security requirements of the rewrite engine. Typically, FollowSymLinks is enabled in the master configuration file for Apache, so it can often be omitted. The Rewriteengine command is used to enable the rewrite engine Ifmodule command to determine if Apache has the Mod_rewrite module installed, and then the author omits the command, but does not mean that it is a good habit. Mod_rewrite will process all URL requests submitted to Apache and match with subsequent rules

Let's start by explaining some examples. ii. using. htaccess to implement URL rewriting (rewrite) and URL redirection (redirect) 1. Map an. htm page to a. PHP

Options +followsymlinks
rewriteengine on
rewriterule ^ (. *) \.htm$ $1.php [NC]

Note: The rewriterule can map an. htm static page to a. PHP dynamic page If you enter through the. htm, the browser address bar displays the. htm extension, but the actual execution on the server is. PHP must ensure that there is a corresponding. php on the server, otherwise it will be 404 Browsers and search engines can access Web pages through. htm and. php at the same time if the. htm exists on the directory, will be ignored [NC] means "case insensitive", more similar definitions please refer to the ". htaccess Regular Expressions" article 2. Temporary redirection (r=302) With permanent redirection (r=301)

Rewriteengine on
rewriterule ^ (. *) \.htm$ $1.php [r,nc,l]

Note: The Rewriterule can redirect the. htm static page to a. PHP dynamic page If you enter through the. htm, the browser address bar will automatically change to. php, which is also the nature of redirection must ensure that the server has a corresponding. PHP, otherwise it will be 404 Browsers and search engines can access Web pages through. htm and. php at the same time if the. htm exists on the directory, the overridden base directory is defined by Rewritebase. For example, if you set up a virtual site under the/var/www directory, deleting this row will cause redirection to http://yourdomain.com/var/www/1.php. Obviously this is not found, and you do not want users to see the directory structure of your server. As an example, if rewritebase/base/, then it will redirect to Http://yourdomain.com/base/1.php. For overriding the base directory, we can also make a direct transformation by turning $1.php into/$1.php, so that rewritebase can be omitted. The letter R indicates a temporary redirect equivalent to [R=302,NC]. For the redirect code, please refer to the HTTP protocol redirection encoding letter L indicates that if this rule is matched, then this rule is the last one, ignoring the following rule.

After discussing r=302 temporary redirection, it's much easier to understand r=301 permanent redirection:

Rewriteengine on
rewriterule ^ (. *) $ http://newdomain.com/$1 [r=301,nc,l]
This rule tells the browser and search engine, the website address has changed permanently, the user's URL request will be sent to the new domain name (host) processing. Because it is redirected to the new host address, Rewritebase does not appear to be necessary. 3. Why use redirection. --the difference between redirection and URL rewritingBy redirecting, the browser knows that the page position changes, thus changing the address bar to display the address through redirection, the search engine realizes that the page has been moved, thus updating the search engine index, removing the previously defunct link from the search results for temporary redirection (r=302) and permanent redirection (r=301) are pro-search engine, is the important technology of SEO URL rewrite is used to map the page to another page of the site, if rewritten to another network host (domain name), then by redirection processing 4. long and short address translation

With URL rewriting, we can easily convert short and long addresses, but it is not appropriate to redirect them.

Rewriteengine on
rewriterule ^grab/public/files/download/download.php

If you visit


The page is executed:

Http://mysite/public/files/download/download.php?file=my.zip 5. Remove www

Options +followsymlinks
rewriteengine on
rewritecond%{http_host} ^www\. (. *) [NC]
rewriterule ^ (. *) $ http://%1/$1 [r=301,nc,l]
6. Plus www
Rewriteengine on
rewritecond%{http_host} ^ (. *) $
rewriterule (. *) http://www\.%1/$1 [r=301,l]
7. Support multi-domain access

If you accidentally bought a host that doesn't support multiple domains, then. htaccess may be able to help you. Now suppose you have the domain name domain-one.com and domain-two.com, and the server root directory has the corresponding folder one and two, then the following rewrite will allow Apache to accept the two domain name request:

#two domains served from one root
. Rewritecond%{http_host} domain-one.com
rewriterule ^ (. *) $/one/$1 [L]

Rewritecond%{http_host} domain-two.com
rewriterule ^ (. *) $/two/$1 [L]
third, rewrite the query string query_string

The query string refers to the part of the URL request that follows the question mark. For example, the bold part of Http://mysite/grab?foo=bar is the query string, where the variable name is foo and the value is bar. 1. Convert query strings using QSA query_string

The QSA flag (query string appending) is used to intercept the query string in the URI, which is implemented by the parentheses regular expression:

Rewriteengine on
rewriterule/pages/(. +)/page.php?page=$1 [QSA]
Will map the request/pages/123?one=two to/page.php?page=123&one=two Note that the bold part is almost the same, except that "question mark" becomes the "and" symbol if there is no QSA flag, then map to/page.php? Page=123. If you do not use a regular expression of parentheses, you do not need QSA, which is already illustrated in the previous section, "Short and long address translation". The parentheses regular expression can intercept the contents of the query string, but if the QSA flag is not turned on, then the "question mark" in/page.php?page=$1 will be stripped and discarded. This feature can be used to implement the "Peel query string"

With QSA, we can map simple link/simple/flat/link/to Server-side.php?first-var=flat&second-var=link

Rewriteengine on
rewriterule ^/([^/]+)/([^/]+]/?/index.php?first-var=$1&second-var=$2 [QSA]
2. Rewrite the query string with Rewritecond query_string
Rewriteengine on
Rewritecond%{query_string} foo= (. *) Rewriterule
^grab (. *)/page.php?bar=%1
The rule converts the access request Http://mysite/grab?foo=bar to Http://mysite/page.php?bar=bar Rewritecond to capture the value of the variable foo in the query string (query_string). and stored in%1

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.