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]
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>
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:
Text:
. Match any single character
[chars] matches the current character
[^chars] does not match the current character
TEXT1|TEXT2 contains Text1 or text2 any one
Quantifiers:
? 0 or one character before the number
* 0 or any arbitrary length of any character
+ one or any character of any length
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:
^ Match content start tag
$ match Content end tag
Escaping:
\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.
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 HTTP Server 2.0 version document http://linux.dalouis.com/doc/apache2.0/