URL重写是截取传入Web请求并自动将请求重定向到其余URL的过程。
好比:php
hostname/list_1,服务器自动将其定向为http://hostname/list.php?id=1
该技术经常使用于引擎优化(SEO)html
URL重写的好处web
伪静态也是重写的一种实现。 URL重写有两种实现正则表达式
实现list.php?Mode=A重写为伪静态list-A.html(假设工做目录为E:/dev/www/php/new)apache
#LoadModule rewrite_module modules/mod_rewrite.so
把前面#去掉。没有则添加,但必选独占一行,使apache支持 mod_rewrite 模块浏览器
<Directory "D:/ApacheServer/web"> # # Possible values for the Options directive are "None", "All", # or any combination of: # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews # # Note that "MultiViews" must be named *explicitly* --- "Options All" # doesn't give it to you. # # The Options directive is both complicated and important. Please see # http://httpd.apache.org/docs/2.2/mod/core.html#options # for more information. # Options Indexes FollowSymLinks # # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit # AllowOverride None # # Controls who can get stuff from this server. # Order allow,deny Allow from all </Directory>
把AllowOverride None 换成 AllowOverride All 使apache支持 .htaccess 文件服务器
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule index.html index.php RewriteRule list-([A-Z+])\.html $list.php?mode=$1 [NC] </IfModule>
注释: RewriteEngine 为重写引擎开关,on为开启,off为关闭。ide
RewriteRule 是路由转向规则,$ 以前路径为浏览器中要输入路径,这里能够用正则表达式表达。$+空格 后路径为后台实际转向路径, 转向后台实际路径时能够传参数,例子里的后台页面能够用$_GET['p'] $_GET['action'] $_GET['id'] 来接收 $1 表明浏览器路径中输入的第一个正则表达式的值,以此类推,$2表明第二个正则表达式的值 RewriteRule 路由转向规则里正则表达式用括号 () 括起来优化