Apache的rewrite模块,url重写模块配置
1,首先检查是否已安装rewrite模块: cat httpd.conf | grep rewrite LoadModule rewrite_module modules/mod_rewrite.so 2,生成伪静态html连接: (1)生成伪静态html 在<VirtualHost>段最后加入 RewriteEngine on RewriteRule /goods([0-9]+).html /goods.php?id=$1 [PT] 更标准的写法为: RewriteRule ^(.*)/goods([0-9]+).html$ $1/goods.php?id=$2 [PT] 更简洁的写法: /goods(\d+)\.html /goods\.php\?id=$1 第一个(0-9]+)对应参数$1,以此类推第二个对应$2 举例: RewriteRule /forum-([0-9]+)-([0-9]+)\.html /forumdisplay.php?fid=$1&page=$2 [PT] 测试http://www.xxx.com/goods1.html 是否与/goods.php?id=1的内容相同 最后将所有链接换成设置后的伪静态html地址方式 [PT]:url全局转换,即转换过的goods31.html对应goods.php?id=31 (默认就是这个不加参数) [R]: url重定向 即使用goods31.html访问时跳转到goods.php?id=31 3,防盗链: RewriteCrond %{HTTP_HOST} !upkiller.com [R=301,L] RewriteRule ^(.*)$ http://www.upkiller.com/warning.html [R=301,L] 把不是来自upkiller.com的请求重定向到http://www.upkiller.com/ 更好的做法: RewriteCond %{HTTP_REFERER} !^http://(www\.)?upkiller\.com/.*$ [NC] RewriteRule \.(mp3|rar|jpe|gif)$ http://www.upkiller.com/warning.jpg [R=301,L] 4,防百度爬虫: RewriteCond %{HTTP_USER_AGENT} ^Baiduspider [OR] RewriteRule ^(.*)$ http://www.google.com/ [R=301,L] 把来自百度的爬虫转到goole |