FloatingRock
FloatingRock

Reputation: 7065

.htaccess: Redirect all pages except one

I'm trying to setup my .htaccess file to redirect from oldsite.com to newsite.com, with just one exception: If the user visits http://oldsite.com/cal then I want it to display the cal.html file in the root directory.

Here's the current .htaccess file that I've got (which doesn't work):

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]

What do I mean by it doesn't work? Well, it just redirects http://oldsite.com/cal to http://newsite.com/cal instead of displaying http://oldsite.com/cal.html

Upvotes: 2

Views: 5575

Answers (1)

zx81
zx81

Reputation: 41838

Change it to this:

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]

Notes

  • if oldsite and newsite live on different servers, you don't even need the RewriteCond.
  • you could also invert the order of the rules, in which case the main rule would be RewriteRule ^(?!cal/?$) http://newsite.com%{REQUEST_URI} [NE,R=301,L]

Upvotes: 3

Related Questions