.htaccess:重定向除一个以外的所有页面

时间:2014-07-20 19:40:37

标签: regex apache .htaccess mod-rewrite redirect

我正在尝试将.htaccess文件设置为从oldsite.com重定向到newsite.com,只有一个例外:如果用户访问http://oldsite.com/cal,那么我希望它在根目录中显示cal.html文件。

这是我得到的当前.htaccess文件(不起作用):

Options +FollowSymlinks -MultiViews
RewriteEngine On

# this page can be served .. (not working)
RewriteRule /cal http://oldsite.com/cal.htm [L,NC]

# .. but rewrite everything else (this works fine)
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com  [NC]
RewriteRule ^(.*)  http://newsite.com/$1     [R=301,L,NC]

它不起作用是什么意思?好吧,它只是将http://oldsite.com/cal重定向到http://newsite.com/cal,而不是显示http://oldsite.com/cal.html

1 个答案:

答案 0 :(得分:3)

将其更改为:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteRule ^cal/? cal.htm [L,NC]

RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com  [NC]
RewriteRule ^ http://newsite.com%{REQUEST_URI} [NE,R=301,L]

注释

  • 如果oldsitenewsite住在不同的服务器上,您甚至不需要RewriteCond。
  • 你也可以颠倒规则的顺序,在这种情况下主要规则是RewriteRule ^(?!cal/?$) http://newsite.com%{REQUEST_URI} [NE,R=301,L]