Using Apache's rewrite technology_PHP tutorial
使用Apache的rewrite技术
做PHP项目中需要用到URL重定向技术,基本上的需求就是把比如 /user/heiyeluren 重定向到 /user.php?uid=heiyeluren 之类的URL上,当然,你也可以把 /article/200707291011.html重定向到 /article.php?id=200507291011 之类的,模拟好像是静态页面,能够隐藏URL真实地址,有助于雨鞋基本的安全防范等等。那么好像rewrite是个很好的解决办法。
要在Apache里运行rewrite的话,必须先安装mod_rewrite的组件,就是一个mod_rewrite.c文件,然后必须在./configure的时候要放上mod_rewrite就能安装。
一般配置rewrite的话,可以在httpd.conf里面配置,也能在网页当前目录的.htaccess文件里进行定义来决定重定向去那个文件,那样的话,就非常具有灵活性了,同样也能够适合虚拟主机用户来做。
我们看一个.htaccess文件的例子:
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_share.php?id=$1&s=1&selTime=$2 [QSA,L]
12 #RewriteRule ^([^/]+)/day/([^/]+)/?$ /user_share.php?id=$1&s=1&selTime=$2 [QSA,L]
13
14 RewriteRule ^([^/]+)/day/([^/]+)/?$ /user_share.php?id=$1&s=1&selTime=$2 [QSA,L]
15 RewriteRule ^([^/]+)/week/([^/]+)/?$ /user_share.php?id=$1&s=2&selTime=$2 [QSA,L]
16 RewRiteRule ^([^/]+)/month/([^/]+)/?$ /user_share.php?id=$1&s=3&selTime=$2 [QSA,L]
17
18 RewriteRule ^([^/]+)/day/?$ /user_share.php?id=$1&s=1 [QSA,L]
19 RewriteRule ^([^/]+)/week/?$ /user_share.php?id=$1&s=2 [QSA,L]
20 RewriteRule ^([^/]+)/month/?$ /user_share.php?id=$1&s=3 [QSA,L]
21
22 RewriteRule ^([^/]+)/?$ /user_share.php?id=$1 [QSA,L]
23
比较长吧,我们就简单关注一下关键的内容。
Text:
. 匹配任意单个字符
[chars] 匹配当前字符
[^chars] 不匹配当前字符
text1|text2 包含text1或者text2任何一个
Quantifiers:
? 零个或者一个?号前的字符
* 零个或者任意个任意长度的任意字符
+ 一个或者任意个任意长度的字符
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:
^ 匹配内容开始标记
$ 匹配内容结束标记
Escaping:
\char 使用\来进行特殊字符的转义,包括".[]()"等字符的转义
基本规则如上,比如下面的正则表达式:^/([^/]+)/~([^/]+)/(.*)$ 就是能够匹配象 /Language/~ Realname/.../File 这样的路径。
那么从这个角度去看上面的内容就比较容易理解了。我们简单看一下RewriteRule指令的规则:
RewriteRule 访问的路径 需要指向的真实路径
这样就很清楚了,比如说我要把/user/heiyeluren重定向到/user.php?uid=heiyeluren那么我的规则就必须这样:
RewriteRule ^user/([^/]+)$ ^/user.php?uid=$2 [QSA,L]
举一反三,就很容易理解规则如何去写,也就能够理解上面规则脚本的内容了。
没有清楚的,请参考附上的链接,更深入的了解具体内容。写的不好请见谅。
Attachment:
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 Version Document http://linux.dalouis .com/doc/apache2.0/

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools