Apache rewrite _ PHP Tutorial

Source: Internet
Author: User
Use Apache rewrite technology. The URL redirection technology is required to use Apache rewrite technology for PHP projects. The basic requirement is to redirect userheiyeluren to user. php? On URLs such as uidheiyeluren, 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
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

For a long time, let's take a look at the key content. RewriteEngine is used to determine whether to run the URL rewriting function. RewriteBase is the basic path. The most important thing is the following RewriteRule, which is the rule we need to rewrite, here, regular expressions compatible with Perl rules are applied:

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 http://www.yujs.com/recommendation/004.htm
Apache module mod_rewrite http://linux.dalouis.com/doc/apache2.0/mod/mod_rewrite.html
URL override guide http://linux.dalouis.com/doc/apache2.0/misc/rewriteguide.html
Apache HTTP Server 2.0 documentation http://linux.dalouis.com/doc/apache2.0/

The URL redirection technology is required for ghost PHP projects. The basic requirement is to redirect/user/heiyeluren to/user. php? Uid = heiyeluren and other URLs ,...

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.