前段时间项目为了配合seo的工作,把现有的php网站改成静态页面,刚拿到需求时候第一感觉就是用静态页面啊,可是看了一会以后发现页面有点多4、50个,没办法就用比较简单的url重写(apache的)吧,


去掉这个前面的#,启用它

LoadModule rewrite_module modules/mod_rewrite.so

AllowOverride None 的None 改成All


然后就是写.htaccess文件了(放在根目录 但是会牺牲微量的访问时间),当然也可以在apache的配置文件中加,但是那个不太灵活(牺牲的时间更短)


实例

#打开重写语句

RewriteEngine On


RewriteRule castrolmagnatec.com.cn/questions/([a-z]+)/(index.html)?$ castrolmagnatec.com.cn/questions/index.php?tag=$1


分析

castrolmagnatec.com.cn/questions/([a-z]+)/(index.html)?$为需要匹配的静态url的正则表达式

castrolmagnatec.com.cn/questions/index.php?tag=$1后面的$1为正则表达式中第一个分组(圆括

号)中匹配的内容


上面的代码可以实现下面的效果
把 域名/castrolmagnatec.com.cn/questions/aaaaa/index.html 的url跳转到

域名/castrolmagnatec.com.cn/questions/index.php?tag=aaaaa

效果就是php的页面在浏览器上显示的url为

域名/castrolmagnatec.com.cn/questions/aaaaa/index.html