URL Rewrite and redirect

Source: Internet
Author: User

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 in # More rules below ... < ;/ifmodule>

Some of the things we need to be aware of:

    • FollowSymLinks must be enabled, which is the security requirement 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
    • The Ifmodule command is used to determine if Apache has installed the Mod_rewrite module, 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 +followsymlinksrewriteengine onrewriterule ^ (. *) \.htm$ $1.php [NC]

Precautions:

    • 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
    • You must ensure that there is a corresponding. php on the server, otherwise it will be 404
    • Browsers and search engines can access Web pages simultaneously through. htm and. php
    • If the. htm is present on the directory, it will be ignored
    • [NC] means "case-insensitive", more similar definitions, please refer to the ". htaccess Regular Expressions" article
2. Temporary redirection (r=302) vs. Permanent redirection (r=301)
Rewritebase/ Rewriterule ^ (. *) \.htm$ $1.php [R, NC,L]

Precautions:

  • The Rewriterule is able to redirect. htm static pages to the. PHP Dynamic page
  • If you enter through the. htm, the browser address bar automatically transitions to. php, which is also the essence of redirection
  • You must ensure that there is a corresponding. php on the server, otherwise it will be 404
  • Browsers and search engines can access Web pages simultaneously through. htm and. php
  • If the. htm is present on the directory, it will be ignored
  • rewritebase defines the overriding base directory .
  • For example, if you set the virtual site under the /var/www directory, deleting this line 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 be redirected 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 redirect encoding on this site.
  • The letter L indicates that if the rule can be 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 onrewriterule ^ (. *) $ 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 redirect? --the difference between redirection and URL rewriting
    • By redirecting, the browser knows the location of the page changes, thus changing the address displayed in the Address bar
    • By redirecting, the search engine realizes that the page has been moved, updating the search engine index, and removing the previously defunct link from the search results.
    • Temporary redirection (r=302) and permanent redirection (r=301) are pro-search engines and are an important technology for SEO
    • URL rewriting is used to map a page to another page of the site, and if overridden to another network host (domain name), it is processed by redirection
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 Onrewriterule ^grab/public/files/download/download.php

If you visit

Http://mysite/grab?file=my.zip

The page is executed:

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

5. Remove www
Options +followsymlinksrewriteengine Onrewritecond%{http_host} ^www\. (.*) [NC] Rewriterule ^ (. *) $ http://%1/$1 [r=301,nc,l]
6. Plus www
Rewriteengine Onrewritecond%{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.comrewritecond%{request_uri}!^/onerewriterule ^ (. *) $/one/$1 [L]RewriteCond%{ Http_host} domain-two.comrewritecond%{request_uri}!^/tworewriterule ^ (. *) $/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. Like, Http://mysite/grab? The bold part of 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 onrewriterule/pages/(. +)/page.php?page=$1 [QSA]
    The
    • will/pages/123?one=two   maps to/page.php?page=123&one=two
    • Notice that the bold part is almost the same, except that the question mark becomes the and symbol
    • if there is no QSA flag, it is mapped to/page.php?page=123 .
    • If you do not use the , you do not need QSA, which is already illustrated in the previous section, "Short and long address translation".
    • can intercept the contents of the query string, but if the QSA flag is not turned on, then /page.php?page=$1 is discarded after it is stripped. This feature can be used to implement a "Peel query string"

With QSA, we can map the simple link/simple/flat/link/To server-side.php?first-var=flat&second-var= Link

Rewriteengine onrewriterule ^/([^/]+)/([^/]+]/? /index.php?first-var=$1&second-var=$2 [QSA]
2. Rewrite the query string with Rewritecond query_string
Rewriteengine Onrewritecond%{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 is used to capture the value of the variable foo in the query string (query_string) and is stored in %1
    • Query_string is an Apache-defined "variable = value" Vector (array)
3.QSA and Rewritecond double Sword Qi fa
Rewriteengine Onrewritecond%{query_string} foo= (. +) Rewriterule ^grab/(. *)/%1/index.php?file=$1 [QSA]
    • Will map the/grab/foobar.zip?level=5&Foo=bar to the/bar/index.php?file=foobar.zip&level=5& Foo=bar
    • The post-conversion root directory is the bar directory
    • Foobar.zip? The "question mark" in Level=5 becomes the "ampersand" in Foobar.zip&level=5
4. Peel the query string

Simply add a "question mark" after the link you want to start stripping, and do not enable the QSA flag to peel the query string

Rewriteengine on# Whatever QS isrewritecond%{query_string}. # I don ' t want it with Question markrewriterule foo.php (. *)/foo.php? L
Iv. access control using Rewritecond and Rewriterule

We have mentioned a lot of useful access control methods in the first Article Htaccess Foundation, in fact, through the rewrite can also achieve similar functions, and can be more powerful!

1. File access control

Access controls previously implemented with order, files, and FilesMatch commands can meet most requirements, but when users are denied they see a huge "403 Forbidden", and if you don't want to hurt the user's feelings, you need to show something else, This feature can be achieved by rewrite:

 rewriteengine Onrewritecond%{request_filename}!^ (. +) \.css$rewritecond%{request_filename}!^ (. +) \.js$ Rewritecond%{request_filename}!special.zip$rewriterule ^ (. +) $/chat/[NC] 
    • The rule will only allow users to request. CSS,. js types of files and Special.zip file
    • Rewriterule later specified restriction rules : Mapping to/char/directory processing
    • Rewritecond "exclamation point" (!) in the back of the Plays the "negation" function, it shows that the rewriterule rule is applied to those who do not meet the following regular expression , that is, no rules will be applied to files of the current type.
    • Rewritecond is a logical "and" connection, that is, only if the three conditions are not satisfied when the execution of Rewriterule
    • The rule also restricts access to. htm,. jpg, and other formats
    • The rule cannot be placed under the root directory (/) of the virtual site, or it will die in a loop
    • If it is a level two directory, such as/test/, then the parameters passed in Rewritecond start with/test/, so the file name obtained from (. +) also contains/test/, which the reader must be cautious about.
    • To get only the file name, you can replace (. +) with ( [^/]+)and remove the symbol ^ as follows:
      Rewriteengine Onrewritecond%{request_filename}! ([^/]+)\.css$rewritecond%{request_filename}! ([^/]+)\.js$rewriterule ^ (. +) $/chat/[NC]
2. Block user-agent with. htaccess

What is User-agent? User-agent is used to "tell" the browser to the server, or, more specifically, all HTTP clients have to use User-agent to "tell" the server so that the server responds differently to different clients. For example, a site may need to respond differently to browsers, search engine crawl, and various download tools. The server is differentiated by the so-called user-agent.
If your server provides downloads for certain resources, you will have to be cautious about downloading software such as "Thunder", as they may suck up your site resources and affect your normal visitor access. To do this, we can use rewrite to restrict access to certain UA:

Rewriteengine Onrewritecond%{http_user_agent} 2.0.50727 [Nc]rewriterule. Abuse.txt [L]
    • This rule restricts the "Thunderbolt" client from downloading resources and resets the download file to Abuse.txt
    • Http_user_agent is an Apache built-in variable
    • 2.0.50727 is the characteristic string of Thunderbolt user-agent
    • The "dot" behind rewriterule means "arbitrary uri", that is, whatever is requested, outputs Abuse.txt

Generally, we do not limit only one UA. Use [OR] to achieve unified processing of multiple UA:

Rewriteengine Onrewritecond%{http_user_agent} 2.0.50727 [Nc,or]rewritecond%{http_user_agent} ^BlackWidow [NC,OR]# etc.. Rewritecond%{http_user_agent} ^net\ Vampire [Nc]rewriterule. Abuse.txt [L]
3. Block hotlinking with. htaccess (hot-linking)

Hotlinking, especially the picture, is very shameful! Even if you copy the image to your own server, it is more than the theft of other people's image links to glory! (Spit Bad)
The rewrite function of the. Htaccess can provide a very simple and effective way to prevent this shameful behavior:

Rewriteengine Onrewritecond%{http_referer}!^ $RewriteCond%{http_referer}!^http://(www\.)? lesca\.me/[Nc]rewritecond%{request_uri}!hotlink\.png [Nc]rewriterule. *\. (gif|jpg|png) $/hotlink.png [NC]

Simply explain the functionality of the rule:

    • In addition to the site other than the website should not be referenced by the picture, the specific can be understood as
    • If the reference site is "empty" or "local", or if the referenced object is "Hotlink.png", then access is allowed
    • Again, the default logical connection Word between rewritecond is logical "with"
    • The difficulty here is to understand the logic transformation, namely the De Morgan law

URL Rewrite and redirect

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.