Using Apache's rewrite technology

Source: Internet
Author: User
Using Apache's rewrite technology

PHP project needs to use the URL redirection technology, the basic requirement is to redirect such as/user/heiyeluren to/user.php?uid=heiyeluren such as the URL, of course, you can also put/article/ 200707291011.html Redirect to/article.php?id=200507291011 and the like, simulation seems to be static page, can hide the real address of the URL, to help the basic safety precautions and so on. So 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 you must put Mod_rewrite on the./configure to install it.
General configuration rewrite, can be configured in the httpd.conf, but also in the current directory of the Web page in the. htaccess file definition to redirect to that file, in that case, it is very 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]
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]
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]
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]
Rewriterule ^ ([^/]+)/?$/user_share.php?id=$1 [qsa,l]

For a long time, let's take a brief look at the key elements. <IfModule></IfModule> is the definition of the content, Rewriteengine is to determine whether to run the URL rewriting function, Rewritebase is the basic path is what, The key is the following rewriterule, which is the rule we need to rewrite, which applies a regular expression that is compatible with Perl rules:

. Match any single character
[chars] matches the current character
[^chars] does not match the current character
TEXT1|TEXT2 contains Text1 or text2 any one

? 0 or one character before the number
* 0 or any arbitrary length of any character
+ one or any character of any length

(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)

^ Match content start tag
$ match Content end tag

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

Basic rules such as the following regular expression: ^/([^/]+)/~ ([^/]+)/(. *) $ is capable of matching paths like/language/~ realname/.../file.

Then it is easier to understand the content from this point of view. Let's take a quick look at the rules of the Rewriterule directive:
The path that rewriterule accesses needs to point to the real path
So it's 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 easy to understand how the rules are written, and you can understand the contents of the above rule scripts.

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

Apache Rewrite Technology Http://
Apache Module Mod_rewrite
URL Rewriting guide
Apache HTTP Server 2.0 version document

  • Related Article

    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: 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.