开发者

Setting host and path in Apache 301 redirect

开发者 https://www.devze.com 2023-03-09 19:19 出处:网络
I need to redirect http://oldsite.com/some/content to http://newsite.com/some/content in .htaccess, but only that page (well, two pages, with the same idea).

I need to redirect http://oldsite.com/some/content to http://newsite.com/some/content in .htaccess, but only that page (well, two pages, with the same idea).

From what I can see, 301 redirects don't take the host, only the path, in the first parameter, and

Redirect 301 /some/content http://newsite/some/content

isn't going to work, as oldsite.com is a ServerAlias of newsite.com (maybe not ideal, but that part is out of my hands), and it seems the rule above leads to a redirect loop.

What's the best way to accomplish this? I can't use a PHP solution as the redirect n开发者_高级运维eeds to execute before another rule further down in the same .htaccess.


You can set an env variable to identify the site, e.g.:

SetEnvIf default-site ^ site=new
SetEnvIfNoCase Host ^oldsite\.com$ site=old

... and use mod_rewrite to perform a conditional redirection:

RewriteCond %{ENV:site} =old [NC]
RewriteRule ^(.*)$ http://newsite.com%{REQUEST_URI} [NE,R=permanent,L]
0

精彩评论

暂无评论...
验证码 换一张
取 消