在 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/{GetProperty(Content)} [R=permanent,L]
# Redirect webhosting-world.com to wbhw.com RewriteCond %{REQUEST_URI} !^/forums/ RewriteRule /(.*) http://www.wbhw.com/{GetProperty(Content)} [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/{GetProperty(Content)} [R=permanent,L]
# Redirect webhosting-world.com to wbhw.com RewriteCond %{REQUEST_URI} !^/forums/ RewriteRule /(.*) http://www.wbhw.com/{GetProperty(Content)} [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/{GetProperty(Content)}/ [R]
RewriteCond %{HTTP_HOST} !^www.zzz.com RewriteRule ^(.+) %{HTTP_HOST} [C] RewriteRule ^([^.]+).zzz.com http://www.zzz.com/user.php?username={GetProperty(Content)}
例子二: /type.php?typeid=* --> /type*.html /type.php?typeid=*&page=* --> /type*page*.html
RewriteRule ^/type([0-9]+).html$ /type.php?typeid={GetProperty(Content)} [PT] RewriteRule ^/type([0-9]+)page([0-9]+).html$ /type.php?typeid={GetProperty(Content)}&page=$2 [PT]