url rewrite实例

我们的目标是把http://www.bulknews.cn/show.php?id=1014700通过rewrite的url重写,使可以直接http://www.bulknews.cn/1014700.html访问
1.首先配置apache的httpd.conf,目的是使.htaccess文件生效
    ServerAdmin a@b.com
    DocumentRoot /var/www/bulknews
    ServerName www.bulknews.cn
      Options FollowSymLinks
      AllowOverride All
ErrorLog  "|/usr/local/sbin/cronolog /var/log/www/wapya/www.bulknews.cn_error_log.%Y%m%d"
CustomLog "|/usr/local/sbin/cronolog /var/log/www/wapya/www.bulknews.cn_access_log.%Y%m%d" combined
注意,这里必须是FollowSymLinks,AllowOverride是All,否则rewrite引擎根本不起作用,另外如果你的apache默认根目录是/var/www 的话,在/var/www的配置字段也要设置
Options FollowSymLinks
AllowOverride All
我的就因为这个,只是在虚拟主机设置,导致一直不能实现静态页面的转化。
2.在/var/www/bulknews下设置.htaccess文件
我们这边最简单的配置就是
RewriteEngine   on
RewriteBase     /
RewriteRule     (.+)/.html$ show.php?id=$1 [L]
OK,重启web服务,这时候你就可以正常访问http://www.bulknews.cn/1014700.html

 

//=========================================================//

 

在 httpd 中将一个域名转发到另一个域名
虚拟主机世界近期更换了域名,新域名为 www.wbhw.com, 更加简短好记。这时需要将原来的域名 webhosting-world.com, 以及论坛所在地址 webhosting-world.com/forums/ 定向到新的域名,以便用户可以找到,并且使原来的论坛 URL 继续有效而不出现 404 未找到,比如原来的 http://www. webhosting-world.com/forums/-f60.html, 让它在新的域名下继续有效,点击后转发到 http://bbs.wbhw.com/-f60.html, 这就需要用 apache的 Mod_ rewrite功能来实现。

在< virtualhost> 中添加下面的重定向规则:

RewriteEngine On
# Redirect webhosting-world.com/forums to bbs.wbhw.com
RewriteCond %{REQUEST_URI} ^/forums/
RewriteRule /forums/(.*) http://bbs.wbhw.com/$1 [R=permanent,L]

# Redirect webhosting-world.com to wbhw.com
RewriteCond %{REQUEST_URI} !^/forums/
RewriteRule /(.*) http://www.wbhw.com/$1 [R=permanent,L]

添加了上面的规则以后, 里的全部内容如下:
< virtualhost *:80>
ServerAlias webhosting-world.com
ServerAdmin admin@webhosting-world.com
DocumentRoot /path/to/webhosting-world/root
ServerName www.webhosting-world.com

RewriteEngine On
# Redirect webhosting-world.com/forums to bbs.wbhw.com
RewriteCond %{REQUEST_URI} ^/forums/
RewriteRule /forums/(.*) http://bbs.wbhw.com/$1 [R=permanent,L]

# Redirect webhosting-world.com to wbhw.com
RewriteCond %{REQUEST_URI} !^/forums/
RewriteRule /(.*) http://www.wbhw.com/$1 [R=permanent,L]
< /virtualhost>




URL重定向例子一:
1.http://www.zzz.com/xxx.php-> http://www.zzz.com/xxx/
2.http://yyy.zzz.com-> http://www.zzz.com/user.php?username=yyy 的功能

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.zzz.com
RewriteCond %{REQUEST_URI} !^user/.php$
RewriteCond %{REQUEST_URI} /.php$
RewriteRule (.*)/.php$ http://www.zzz.com/$1/ [R]

RewriteCond %{HTTP_HOST} !^www.zzz.com
RewriteRule ^(.+) %{HTTP_HOST} [C]
RewriteRule ^([^/.]+)/.zzz/.com http://www.zzz.com/user.php?username=$1


例子二:
/type.php?typeid=* --> /type*.html
/type.php?typeid=*&page=* --> /type*page*.html

RewriteRule ^/type([0-9]+).html$ /type.php?typeid=$1 [PT]
RewriteRule ^/type([0-9]+)page([0-9]+).html$ /type.php?typeid=$1&page=$2 [PT]

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值