apacherewrite:Apache rewrite例子集合

在 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, 这就需要用=hilite1>apache Mod_=hilite2>rewrite功能来实现

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

=hilite2>RewriteEngine _disibledevent=>RewriteCond %{REQUEST_URI} ^/forums/
=hilite2>RewriteRule /forums/(.*) http://bbs.wbhw.com/$1 [R=permanent,L]

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

添加了上面规则以后全部内容如下:
< virtualhost *:80>
ServerAlias webhosting-world.com
ServerAdmin [email protected]
DocumentRoot /path/to/webhosting-world/root
ServerName www.webhosting-world.com

=hilite2>RewriteEngine _disibledevent=>Rewrite
Cond %{REQUEST_URI} ^/forums/
=hilite2>RewriteRule /forums/(.*) http://bbs.wbhw.com/$1 [R=permanent,L]

# Redirect webhosting-world.com to wbhw.com
=hilite2>RewriteCond %{REQUEST_URI} !^/forums/
=hilite2>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 功能

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

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

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

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

Tags:  apachemodrewrite apacheurlrewrite apacherewrite配置 apacherewrite

延伸阅读

最新评论

发表评论