301 Redirect except for one person using .htaccess?
I recently migrated a WordPress site to a new server and new domain name. To redirect traff开发者_StackOverflow社区ic from the old site to the new, I put in place a simple one-line .htaccess file:
Redirect 301 / http://www.newsite.com/
Now, however, the client wants access to the old site. Is there a way that I can let one person in and redirect everyone else to the new site?
You can make your redirect conditional by adding a RewriteCond directive before the Redirect directive:
RewriteCond %{SOMETHING} !the-client
Where SOMETHING is a server variable you can use to identify the client, and the-client is a pattern that identifies the client.
For example, if your client always comes from a known IP address, you could use that:
RewriteCond %{REMOTE_ADDR} !^12\.34\.56\.78$
Redirect 301 / http://www.newsite.com/
See http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html for more of the server variables that are available if IP address doesn't work for your situation.
If the client has a (relatively) static IP address, you could do something like this in place of what you currently have:
RewriteEngine On
# Replace with the appropriate IP address
RewriteCond %{REMOTE_ADDR} !=192.168.0.1
RewriteRule .* http://www.newsite.com%{REQUEST_URI} [R=301,L]
Alternatively, you might be able to do something like this...
RewriteEngine On
RewriteCond %{THE_REQUEST} !\?noredirect
RewriteCond %{HTTP_REFERER} !^http://www\.oldsite\.com
RewriteRule .* http://www.newsite.com%{REQUEST_URI} [R=301,L]
And then (assuming their browser sends the referrer) they could access the old site at http://www.oldsite.com/?noredirect and hopefully any subsequent links they clicked after that would work correctly without redirecting to the new site. I didn't test this though, so I may have overlooked something.
精彩评论