Using Apache's rewrite technology

Source: Internet
Author: User

using Apache's rewrite technology

To do PHP projects need to use URL redirection technology, the basic requirement is to redirect analogy/user/heiyeluren to the URL such as/user.php?uid=heiyeluren, of course, you can also put/article/ 200707291011.html Redirect to/article.php?id=200507291011 and the like, the simulation is like a static page, you can hide the URL real address, to help the main safety precautions and so on. So it seems that rewrite is a very good way to solve it.

To execute rewrite in Apache, you must first install the Mod_rewrite component, which is a mod_rewrite.c file, and then you must put the mod_rewrite on the./configure to install it.
General configuration rewrite, can be configured in the httpd.conf, but also in the Web page of the current folder in the. htaccess file to define to redirect to that file, in that case, it is flexible, the same can be suitable for virtual host users to do.

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]
Ten Rewriterule ^city/([^/]+)/?$/user_city.php?tag=$1 [qsa,l]
One #RewriteRule ^ ([^/]+)/day/([^/]+)/?$/user_share.php?id=$1&s=1&seltime=$2 [qsa,l]
#RewriteRule ^ ([^/]+)/day/([^/]+)/?$/user_share.php?id=$1&s=1&seltime=$2 [qsa,l]
13
Rewriterule ^ ([^/]+)/day/([^/]+)/?$/user_share.php?id=$1&s=1&seltime=$2 [qsa,l]
Rewriterule ^ ([^/]+)/week/([^/]+)/?$/user_share.php?id=$1&s=2&seltime=$2 [qsa,l]
Rewriterule ^ ([^/]+)/month/([^/]+)/?$/user_share.php?id=$1&s=3&seltime=$2 [qsa,l]
17
Rewriterule ^ ([^/]+)/day/?$/user_share.php?id=$1&s=1 [qsa,l]
Rewriterule ^ ([^/]+)/week/?$/user_share.php?id=$1&s=2 [qsa,l]
Rewriterule ^ ([^/]+)/month/?$/user_share.php?id=$1&s=3 [qsa,l]
21st
Rewriterule ^ ([^/]+)/?$/user_share.php?id=$1 [qsa,l]
</IfModule>

It's a bit longer, so let's take a quick look at the key things. Between <IfModule></IfModule> is the definition of content, Rewriteengine is to determine whether to perform URL rewriting function, Rewritebase is the main path is what, The key is the following rewriterule, which is the rule that we need to rewrite, which applies the rules that are compatible with Perl rules:

Text:
. Match random individual characters
[chars] matches the current character
[^chars] does not match the current character
TEXT1|TEXT2, including Text1 or text2, no matter what.

Quantifiers:
? 0 or one character before the number
* 0 or random random-length characters
+ one or a random length of characters

Grouping:
(text) Grouping of text
(either to set the borders of a alternative or
For making backreferences where the Nth group can
be used on the RHS of a rewriterule with $N)

Anchors:
^ start tag for matching content
$ match Content end tag

Escaping:
/char uses/to escape special characters, including ". Escape of characters such as [] () "

The basic rules are as follows: ^/([^/]+)/~ ([^/]+)/(. *) $ is a path that can be matched like/language/~ realname/.../file.

So from this point of view the above content is easier to understand. Let's take a quick look at the rules of the Rewriterule directive:
The path that rewriterule access needs to point to
It's very clear, for example, that I'm redirecting/user/heiyeluren to/user.php?uid=heiyeluren so my rules have to be like this:
Rewriterule ^user/([^/]+) $ ^/user.php?uid=$2 [qsa,l]

Extrapolate, it is very easy to understand how to write rules, but also to understand the contents of the above rules script.

Not clear, please refer to the attached link for more in-depth understanding of the details. Please forgive me for not writing well.


Report:
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 Rewriting guide http://linux.dalouis.com/doc/apache2.0/misc/rewriteguide.html
Apache Httpserver 2.0 version number document http://linux.dalouis.com/doc/apache2.0/

Using Apache's rewrite technology

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.