Use Apache rewrite Technology

Source: Internet
Author: User

Use Apache rewrite Technology

The URL redirection technology is required for PHP projects. The basic requirement is to redirect/user/heiyeluren to/user. php? Uid = heiyeluren and other URLs. Of course, you can also redirect/article/200707291011.html to/Article. php? Id = 200507291011 and so on. Simulating a static page can hide the real URL address, which helps the basic security of the rain boots. It seems that rewrite is a good solution.

To run rewrite in Apache, you must first install the mod_rewrite component, which is a mod_rewrite.c file, and then install mod_rewrite in./configure.
Generally, you can configure rewrite in httpd. the configuration in conf can also be found in the current directory of the webpage. define in the htaccess file to decide to redirect to that file. In this case, it is very flexible and can also be used by VM users.

Let's look at an example of A. htaccess file:

1 <ifmodule mod_rewrite.c>
2 rewriteengine on
3 rewritebase/
4 rewritecond % {request_filename}-f [or]
5 rewritecond % {request_filename}-d
6 rewriterule ^. * $-[S = 42]
7
8 # rewriterule ^ share/$/share. php [QSA, l]
9 rewriterule ^ tag/([^/] + )/? $/User_tags.php? Tag = $1 [QSA, l]
10 rewriterule ^ city/([^/] + )/? $/User_city.php? Tag = $1 [QSA, l]
11 # rewriterule ^ ([^/] +)/day/([^/] + )/? $/User_assist.php? Id = $1 & s = 1 & seltime = $2 [QSA, l]
12 # rewriterule ^ ([^/] +)/day/([^/] + )/? $/User_assist.php? Id = $1 & s = 1 & seltime = $2 [QSA, l]
13
14 rewriterule ^ ([^/] +)/day/([^/] + )/? $/User_assist.php? Id = $1 & s = 1 & seltime = $2 [QSA, l]
15 rewriterule ^ ([^/] +)/week/([^/] + )/? $/User_assist.php? Id = $1 & s = 2 & seltime = $2 [QSA, l]
16 rewriterule ^ ([^/] +)/month/([^/] + )/? $/User_assist.php? Id = $1 & s = 3 & seltime = $2 [QSA, l]
17
18 rewriterule ^ ([^/] +)/day /? $/User_assist.php? Id = $1 & s = 1 [QSA, l]
19 rewriterule ^ ([^/] +)/week /? $/User_assist.php? Id = $1 & s = 2 [QSA, l]
20 rewriterule ^ ([^/] +)/month /? $/User_assist.php? Id = $1 & s = 3 [QSA, l]
21
22 rewriterule ^ ([^/] + )/? $/User_assist.php? Id = $1 [QSA, l]
23 </ifmodule>

For a long time, let's take a look at the key content. <Ifmodule> </ifmodule> is defined. rewriteengine is used to determine whether to run the URL rewrite function. rewritebase is the basic path. The most important thing is the rewriterule below, this is the rule we need to rewrite. Here we apply a regular expression compatible with Perl rules:

Text:
. Match any single character
[Chars] match the current character
[^ Chars] does not match the current character
Text1 | text2 contains either text1 or text2

Quantifiers:
? Zero or one? Character before
* Zero or any character of any length
+ One or any character of any length

Grouping:
(Text) Grouping of Text
(Either to set the borders of an alternative or
For making backreferences where the nth group can
Be used on the rhs of a rewriterule with $ N)

Anchors:
^ Start marking of matched content
$ End tag of Matching content

Escaping:
\ Char uses \ to escape special characters, including escape characters such ". [] ()"

The basic rules are as follows, for example, the following regular expression ^/([^/] + )/~ ([^/] +)/(. *) $ Can match the image/Language /~ Realname/.../file.

From this perspective, it is easier to understand the above content. Let's take a brief look at the rules of the rewriterule command:
The actual path to which the rewriterule is accessed
This makes it clear. For example, I want to redirect/user/heiyeluren to/user. php? Uid = heiyeluren so my rules must be as follows:
Rewriterule ^ user/([^/] +) $ ^/user. php? Uid = $2 [QSA, l]

In other words, it is easy to understand how rules are written, so that you can understand the content of the above rule script.

For more information, see the link attached. Sorry.

Appendix:
Apache rewrite technology http://www.yujs.com/recommendation/004.htm
Apache module mod_rewrite http://linux.dalouis.com/doc/apache2.0/mod/mod_rewrite.html
URL rewrite guide http://linux.dalouis.com/doc/apache2.0/misc/rewriteguide.html
Apache HTTP Server 2.0 documentation http://linux.dalouis.com/doc/apache2.0/

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.