URL rewriting and Redirect configuration in htaccess

Source: Internet
Author: User
Tags regular expression zip


URL redirection is the main feature of. htaccess. It can convert long addresses into short addresses, convert dynamic addresses into static addresses, redirect lost pages, prevent leeching, and implement automatic language conversion. I think it is difficult to use and understand regular expressions. For more information about the usage of htaccess regular expressions, see. htaccess regular expressions.

I. Preparations start: mod_rewrite

The module that implements all these magical functions is called mod_rewrite. Make sure that this module is installed and enabled on your server:
Sudo a2enmod rewrite
We usually place all the code that involves URL rewriting or redirection as follows:
<IfModule mod_rewrite.c>
# Turn on rewrite engine
Options + FollowSymlinks
RewriteEngine on
# More rules below
...
</IfModule>

Notes:

FollowSymlinks must be enabled, which is the security requirement of the rewrite engine.
FollowSymlinks is usually enabled in the main configuration file of Apache, so it can be omitted.
The RewriteEngine command is used to enable the rewrite engine.
The IfModule command is used to determine whether the mod_rewrite module is installed in Apache. The command will be omitted later, but it does not mean that this is a good habit.
Mod_rewrite processes all URL requests submitted to Apache and matches with subsequent rules.
Here are some examples.

2. Implement URL rewriting and URL redirection using. htaccess)

1. Map the .htm page to. php
Options + FollowSymlinks
RewriteEngine on
RewriteRule ^ (. *) \. htm $ 1.php [NC]
Note:
This rewriterulecan map .htm static pages to. php dynamic pages.
If you access through .htm, the browser address bar displays the .htm extension, but the actual execution on the server is. php.
Make sure there is a corresponding. php on the server; otherwise, 404
The browser and search engine can access the web page through .htm and. php at the same time.
If the directory contains .htm, it will be ignored.
[NC] indicates "case-insensitive". For more similar definitions, see the. htaccess regular expression article on this site.

2. Temporary redirect (R = 302) and permanent redirect (R = 301)

RewriteEngine on
RewriteBase/
RewriteRule ^ (. *) \. htm $ 1.php [R, NC, L]
Note:
This rewriterulecan redirect .htm static pages to. php dynamic pages.
If you access through .htm, the browser's address bar will be automatically converted to. php, which is also the essence of redirection.
Make sure there is a corresponding. php on the server; otherwise, 404
The browser and search engine can access the web page through .htm and. php at the same time.
If the directory contains .htm, it will be ignored.
RewriteBase defines the override benchmark directory.
For example, if you set a virtual site under the/var/www Directory, deleting this line will cause redirection to http://yourdomain.com/var/www/1.php. Obviously this cannot be found, and you do not want users to see the directory structure of your server.
For another example, if RewriteBase/base/is used, it will be redirected to http://yourdomain.com/base/1.php.
You can also directly change $1. php to/$1. php to overwrite the base directory, and then omit RewriteBase.
The letter R indicates temporary redirection, which is equivalent to [R = 302, NC]. For more information about the redirection code, see HTTP redirection code on this site.
The letter L indicates that if the rule can be matched, the rule is the Last one (Last) and the subsequent rule is ignored.
After discussing the temporary redirection of R = 302, it is much easier to understand the permanent redirection of R = 301:
RewriteEngine on
RewriteRule ^ (. *) $ http://newdomain.com/#1 [R = 301, NC, L]
This rule tells the browser and the search engine that the website address is changed permanently, and the user's URL request will be sent to the new domain name (host) for processing.
RewriteBase is unnecessary because it is redirected to a new host address.

3. Why use redirection? -- What is the difference between redirection and URL rewriting?

Through redirection, the browser knows that the page location has changed, thus changing the address displayed in the address bar
Through redirection, the search engine realizes that the page has been moved, so as to update the search engine index and remove the original invalid link from the search results.
Temporary redirection (R = 302) and permanent redirection (R = 301) are both pro-search engines and are an important SEO technology.
URL rewriting is used to map a page to another page on this site. If it is rewritten to another network host (domain name), it will be processed by redirection.

4. Long/short address conversion

Using URL rewriting, we can easily convert long and short addresses, but it is not appropriate to use redirection.
RewriteEngine On
RewriteRule ^ grab/public/files/download. php
If
Http: // mysite/grab? Filedeskmy.zip
The page will be executed:
Http: // mysite/public/files/download. php? Filedeskmy.zip

5. Remove www

Options + FollowSymlinks
RewriteEngine on
RewriteCond % {HTTP_HOST} ^ www \. (. *) [NC]
RewriteRule ^ (. *) $ http: // % 1/$1 [R = 301, NC, L]
6. Add www
RewriteEngine On
RewriteCond % {HTTP_HOST} ^ (. *) $
RewriteRule (. *) http: // www \. % 1/$1 [R = 301, L]

7. Support for multi-domain access

If you accidentally purchase a host that does not support multiple domain names,. htaccess may help you. Now if you have a domain name domain-one.com and a domain-two.com, and there is a corresponding folder one and two in the root directory of the server, then the following rewrite will allow Apache to simultaneously accept requests for two domain names:
# Two domains served from one root ..
RewriteCond % {HTTP_HOST} domain-one.com
RewriteCond % {REQUEST_URI }! ^/One
RewriteRule ^ (. *) $/one/$1 [L]

RewriteCond % {HTTP_HOST} domain-two.com
RewriteCond % {REQUEST_URI }! ^/Two
RewriteRule ^ (. *) $/two/$1 [L]

3. Rewrite the query string QUERY_STRING

A query string is the part after the question mark in a URL request. For example, http: // mysite/grab? The bold part in foo = bar is the query string, where the variable name is foo and the value is bar.
1. Use QSA to convert and query the string QUERY_STRING
The QSA flag (Query String Appending) is used to intercept Query strings in URIs. This truncation operation is implemented using the regular expression in parentheses:
RewriteEngine On
RewriteRule/pages/(. +)/page. php? Page = $1 [QSA]
The request/pages/123? One = two ING to/page. php? Page = 123 & one = two
Note that the bold part is almost the same, except that the "question mark" is changed to the "and" symbol.
If there is no QSA flag, it will be mapped to/page. php? Page = 123.
If you do not use the parentheses regular expression, you do not need to use QSA. This has been demonstrated in the "long-short address translation" section above.
The regular expression in parentheses can intercept the content in the query string, but if the QSA flag is not enabled, then in/page. php? The question mark in page = $1 will be stripped and discarded. This feature can be used to "strip query strings"
Through QSA, we can map simple links/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. Use RewriteCond to rewrite the query string QUERY_STRING

RewriteEngine On
RewriteCond % {QUERY_STRING} foo = (.*)
RewriteRule ^ grab (. *)/page. php? Bar = % 1
This rule will request http: // mysite/grab? Foo = bar to http: // mysite/page. php? Bar = bar
RewriteCond is used to capture the value of foo in the query string (QUERY_STRING) and store it in % 1.
QUERY_STRING is the "variable = value" vector (array) defined by Apache)
3. QSA and RewriteCond
RewriteEngine On
RewriteCond % {QUERY_STRING} foo = (. +)
RewriteRule ^ grab/(. *)/% 1/index. php? File = $1 [QSA]
Will/grab/foobar.zip? Level = 5 & foo = bar ING to/bar/index. php? Filedeskfoobar.zip & level = 5 & foo = bar
After conversion, the root directory is the bar directory.
Foobar.zip? The question mark in level1_5became the "and" symbol in foobar.zip & level = 5.

4. Strip the query string

You only need to add a "question mark" after the link to be stripped, and do not enable the QSA flag, you can strip the query string
RewriteEngine On
# Whatever QS is
RewriteCond % {QUERY_STRING }.
# I don't want it with Question mark
RewriteRule foo. php (. *)/foo. php? [L]
4. Use RewriteCond and RewriteRule for access control
We mentioned many useful access control methods in the first article. htaccess basics. In fact, similar functions can be implemented through Rewrite, and they can be more powerful!

1. File access control

Previously, the access control implemented by the Order, Files, and FilesMatch commands can meet most of the requirements. However, when users are rejected, they see the huge "403 Forbidden ", if you don't want to hurt your feelings, you need to display something else. You can achieve this through Rewrite:
RewriteEngine On
RewriteCond % {REQUEST_FILENAME }! ^ (. +) \. Css $
RewriteCond % {REQUEST_FILENAME }! ^ (. +) \. Js $
RewriteCond % {REQUEST_FILENAME }! Special.zip $
RewriteRule ^ (. +) $/chat/[NC]
This rule will only allow users to request .css, .js files, and also include special.zip files.
The restriction rule is specified after RewriteRule: it is mapped to the/char/directory for processing.
"Exclamation point" (!) after RewriteCond "(!) It indicates that the RewriteRule rule is applied to those who do not meet the subsequent regular expression, that is, the rules are not applied to the current type of files.
RewriteCond is connected by a logical "and", that is, the RewriteRule is executed only when none of the three conditions are met.
This rule will also be restricted to. HTM,. jpg, and other formats.
This rule cannot be placed under the root directory (/) of the virtual site, otherwise it will be endless
If it is a level-2 Directory, such as/test/, then the input RewriteCond parameter starts with/test (. +) the obtained file name also contains/test/. You must be careful with this.
To obtain only the file name, replace (. +) with ([^/] +) and remove the symbol ^, as shown below:
RewriteEngine On
RewriteCond % {REQUEST_FILENAME }! ([^/] +) \. Css $
RewriteCond % {REQUEST_FILENAME }! ([^/] +) \. Js $
RewriteRule ^ (. +) $/chat/[NC]

2. Use. htaccess to block User-agent

What is User-agent? The User-agent is used by the browser to "report the door" to the server. More specifically, all HTTP clients must use the User-agent to "report the door" to the server ", so that the server can make different responses to different clients. For example, a website may need to make different responses to browsers, search engines, crawl, and various download tools. Servers are differentiated by the so-called User-agent.
If your server provides download of some resources, you must be more careful with download software such as "Thunder", because they may suck up your website resources, and affects normal visitor access. To this end, we can use Rewrite to restrict access to certain UA:
RewriteEngine on
RewriteCond % {HTTP_USER_AGENT} 2.0.50727 [NC]
RewriteRule. abuse.txt [L]
This rule restricts the use of resources on the Alibaba Cloud Lightning client, and re-uploads the downloaded files to abuse.txt.
HTTP_USER_AGENT is the built-in variable of Apache.

2.0.50727 is the character string of the Thunder User-agent.

When rewriteruleis followed, all requests are sent to abuse.txt.
Generally, we do not limit only one UA. [OR] can be used to achieve unified processing of multiple UA:
RewriteEngine on
RewriteCond % {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. Use. htaccess to prevent leeching (hot-linking)

Leeching, especially pictures, is shameful! Even copying images to your server is more glorious than stealing others' image links! (Spit out)
The Rewrite function of. htaccess can provide very simple and effective methods to prevent such shameful behavior:
RewriteEngine On
RewriteCond % {HTTP_REFERER }! ^ $
RewriteCond % {HTTP_REFERER }! ^ Http: // (www \.)? Lesca \. me/[NC]
RewriteCond % {REQUEST_URI }! Hotlink \. png [NC]
RewriteRule. * \. (gif | jpg | png) $/hotlink.png [NC]
Briefly explain the functions of the rule:
Images on this site cannot be referenced by other websites except this site.
If the reference site is empty, and the reference object is hotlink.png, access is allowed.
Again, the default logical connector between RewriteCond is logical "and"

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.