伪静态技术:
在我们实际开发中有需求,不希望使用真静态,但是有希望利于SEO,可以考虑用伪静态
http://localhost/news.php?type=music&id=100
我们希望这个地址可以用下面的url 来替换
http://localhost/news-music-id100.html
实现方式 有两种
1.使用正则表达式
2.使用apache 自带的 rewrite 机制完成
正则规范
/规范/i i在匹配中不区分大小写
/规范/x i在匹配中不区分大小写
/d 表示任意一个数字
(/d) 表示一个子表达式
/1 表示一个子表达式的内容,应该出现在这
$str="1111";
$pattern="/(\d)1{3}/i";
preg_match($pattern, $str,$all);
结果
array (size=2)
0 => string '1111' (length=4)
1 => string '1' (length=1)
特殊的说明
(?:pattam) 非捕获匹配
修改后
$str="1111";
$pattern="/(?:\d)1{3}/i";
preg_match($pattern, $str,$all);
结果
array (size=1)
0 => string '1111' (length=4)
(?!pattam) 表示所在位置右侧不能匹配
$str="1112";
$pattern="/(1){3}(?!2)/i";
preg_match($pattern, $str,$all);
结果
array (size=0)
empty
(?<!pattam) 表示位置左侧不能匹配
(?=pattam)
(?<=pattam) 所在位置左侧能够匹配
httpd.conf
搜索rewrite
启用rewrite
LoadModule rewrite_module modules/mod_rewrite.so
打开虚拟主机文件
Include conf/extra/httpd-vhosts.conf
访问的权限如果在本目录中未找到 会追踪到上一级目录上
<VirtualHost *:80>
DocumentRoot "E:/wamp/www/edu"
ServerName www.wangmutian.com
#Directory 配置节点 用于指定该目录下的文件或是其他资源或是图片的访问权限
#设置虚拟虚拟主机的错误页面,欢迎页面 404错误页面
<Directory "E:/wamp/www/edu">
#这里可以指定是否被人访问Allow Deny
Allow from all
#是否列出文件目录结构
#允许indexes 不允许为none
#Options none
#如何配置网站的首页面
DirectoryIndex new.php
#如何配置404错误页面
errorDocument 404 /404.html
#AllowOverride none
#Require all denied
#配置我们的Rewrite规则 on 启动 (网站的根目录下不能有.htaccess)
RewriteEngine On
#rewrite 规则
#RewriteRule aaa.html new.php
RewriteRule new-([a-zA-Z]+)-id(\d+)\.html$ new.php
#RewriteRule new-music-id100\.html new.php
#如果你配置了allowoverride all这表示到对应的目录的.htaccess去匹配规则
allowoverride all
</Directory>
</VirtualHost>
httpd.conf 中:
Options Indexes FollowSymLinks
Order Allow,Deny #表示先看Allow 后看Deny 留下的就是可以访问的
Deny from all
Allow from 127.0.0.1
</Directory>
.htaccess
<IfModule rewrite_module>
#如果rewrite_module 启动了
#配置我们的Rewrite规则 on 启动
RewriteEngine On
#rewrite 规则
#可以写两个规则 如果第一个规则没有找到会去找的个规则
RewriteRule aaa.html new.php
RewriteRule news-([a-zA-Z]+)-id(\d+)\.html$ new.php?type=$1&id=$2
#RewriteRule news-music-id100\.html new.php
</IfModule>
<ifModule rewrite_module>
RewriteEngine On
F:forbidden 强制地址被禁用
RewriteCond %{HTTP-REFERER} !-f
#如果你访问的是一个jpg的图片就禁止
RewriteRule .*\.jpg -[F]
</ifModule>
Apache中RewriteCond就像程序中的if语句一样,表示如果符合某个或某几个条件则执行RewriteCond下面紧邻的RewriteRule语句,这就是RewriteCond最原始、基础的功能,为了方便理解,下面来看看几个例子。
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/5.0.*
RewriteRule index.php index.m.php
RewriteCond %{HTTP_USER_AGENT} ^Lynx.*
RewriteRule index.php index.L.php
RewriteRule index.php index.b.php
上面语句的作用是当你是用FF浏览器访问index.php这个文件的时候,会自动让你访问到index.m.php这个文件,当你是用一些移动终端访问的 时候,会让你对index.php这个文件的访问实际访问的是index.L.php去,如果你是用其它的浏览器访问的时候,会让你跳到 index.b.php。在说形象一点,上面的语句就等同于程序里面的下面语句(依PHP语句为例):
if($_SERVER['HTTP_USER_AGENT'] == 'Mozilla/5.0')
{
//跳转到对index.m.php的访问
}
else if($_SERVER['HTTP_USER_AGENT'] == 'Lynx')
{
//跳转到对index.L.php的访问
}
else
//跳转到对index.b.php的访问
下面是自己收藏的一些有用的重写规则:
RewriteCond %{REQUEST_FILENAME} !-f //如果文件存在,就直接访问文件,不进行下面的RewriteRule.(不是文件或文件不存在就执行重写)
RewriteCond %{REQUEST_FILENAME} !-d //#如果目录存在就直接访问目录不进行RewriteRule
RewriteCond %{REQUEST_URI} !^.*(.css|.js|.gif|.png|.jpg|.jpeg)$ //#如果是这些后缀的文件,就直接访问文件,不进行Rewrite。
在我们实际开发中有需求,不希望使用真静态,但是有希望利于SEO,可以考虑用伪静态
http://localhost/news.php?type=music&id=100
我们希望这个地址可以用下面的url 来替换
http://localhost/news-music-id100.html
实现方式 有两种
1.使用正则表达式
2.使用apache 自带的 rewrite 机制完成
正则规范
/规范/i i在匹配中不区分大小写
/规范/x i在匹配中不区分大小写
/d 表示任意一个数字
(/d) 表示一个子表达式
/1 表示一个子表达式的内容,应该出现在这
$str="1111";
$pattern="/(\d)1{3}/i";
preg_match($pattern, $str,$all);
结果
array (size=2)
0 => string '1111' (length=4)
1 => string '1' (length=1)
特殊的说明
(?:pattam) 非捕获匹配
修改后
$str="1111";
$pattern="/(?:\d)1{3}/i";
preg_match($pattern, $str,$all);
结果
array (size=1)
0 => string '1111' (length=4)
(?!pattam) 表示所在位置右侧不能匹配
$str="1112";
$pattern="/(1){3}(?!2)/i";
preg_match($pattern, $str,$all);
结果
array (size=0)
empty
(?<!pattam) 表示位置左侧不能匹配
(?=pattam)
(?<=pattam) 所在位置左侧能够匹配
httpd.conf
搜索rewrite
启用rewrite
LoadModule rewrite_module modules/mod_rewrite.so
打开虚拟主机文件
Include conf/extra/httpd-vhosts.conf
访问的权限如果在本目录中未找到 会追踪到上一级目录上
<VirtualHost *:80>
DocumentRoot "E:/wamp/www/edu"
ServerName www.wangmutian.com
#Directory 配置节点 用于指定该目录下的文件或是其他资源或是图片的访问权限
#设置虚拟虚拟主机的错误页面,欢迎页面 404错误页面
<Directory "E:/wamp/www/edu">
#这里可以指定是否被人访问Allow Deny
Allow from all
#是否列出文件目录结构
#允许indexes 不允许为none
#Options none
#如何配置网站的首页面
DirectoryIndex new.php
#如何配置404错误页面
errorDocument 404 /404.html
#AllowOverride none
#Require all denied
#配置我们的Rewrite规则 on 启动 (网站的根目录下不能有.htaccess)
RewriteEngine On
#rewrite 规则
#RewriteRule aaa.html new.php
RewriteRule new-([a-zA-Z]+)-id(\d+)\.html$ new.php
#RewriteRule new-music-id100\.html new.php
#如果你配置了allowoverride all这表示到对应的目录的.htaccess去匹配规则
allowoverride all
</Directory>
</VirtualHost>
httpd.conf 中:
Options Indexes FollowSymLinks
Order Allow,Deny #表示先看Allow 后看Deny 留下的就是可以访问的
Deny from all
Allow from 127.0.0.1
</Directory>
.htaccess
<IfModule rewrite_module>
#如果rewrite_module 启动了
#配置我们的Rewrite规则 on 启动
RewriteEngine On
#rewrite 规则
#可以写两个规则 如果第一个规则没有找到会去找的个规则
RewriteRule aaa.html new.php
RewriteRule news-([a-zA-Z]+)-id(\d+)\.html$ new.php?type=$1&id=$2
#RewriteRule news-music-id100\.html new.php
</IfModule>
<ifModule rewrite_module>
RewriteEngine On
F:forbidden 强制地址被禁用
RewriteCond %{HTTP-REFERER} !-f
#如果你访问的是一个jpg的图片就禁止
RewriteRule .*\.jpg -[F]
</ifModule>
Apache中RewriteCond就像程序中的if语句一样,表示如果符合某个或某几个条件则执行RewriteCond下面紧邻的RewriteRule语句,这就是RewriteCond最原始、基础的功能,为了方便理解,下面来看看几个例子。
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/5.0.*
RewriteRule index.php index.m.php
RewriteCond %{HTTP_USER_AGENT} ^Lynx.*
RewriteRule index.php index.L.php
RewriteRule index.php index.b.php
上面语句的作用是当你是用FF浏览器访问index.php这个文件的时候,会自动让你访问到index.m.php这个文件,当你是用一些移动终端访问的 时候,会让你对index.php这个文件的访问实际访问的是index.L.php去,如果你是用其它的浏览器访问的时候,会让你跳到 index.b.php。在说形象一点,上面的语句就等同于程序里面的下面语句(依PHP语句为例):
if($_SERVER['HTTP_USER_AGENT'] == 'Mozilla/5.0')
{
//跳转到对index.m.php的访问
}
else if($_SERVER['HTTP_USER_AGENT'] == 'Lynx')
{
//跳转到对index.L.php的访问
}
else
//跳转到对index.b.php的访问
下面是自己收藏的一些有用的重写规则:
RewriteCond %{REQUEST_FILENAME} !-f //如果文件存在,就直接访问文件,不进行下面的RewriteRule.(不是文件或文件不存在就执行重写)
RewriteCond %{REQUEST_FILENAME} !-d //#如果目录存在就直接访问目录不进行RewriteRule
RewriteCond %{REQUEST_URI} !^.*(.css|.js|.gif|.png|.jpg|.jpeg)$ //#如果是这些后缀的文件,就直接访问文件,不进行Rewrite。