Using mod_rewrite to redirect home page ONLY

时间:2015-05-04 19:36:55

标签: apache .htaccess mod-rewrite redirect

I have a website that needs redirected, but I can't just redirect the / directory because there are other websites in folders on the server, and doing that redirects them as well. NOT GOOD!

So I have my .htaccess file with a bunch of 301 redirects for individual HTML pages, and those work fine. But I need to redirect the home page. Here is what I have to do that:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^recherchegoldens.com [NC]
RewriteRule ^(.*)$ http://whitegoldenretriever.com/$1 [R=301,L]

That forwards the home page, great. But it also messes up my other 301 redirects. Here is one of my redirects:

Redirect 301 /Available-Pups.html http://www.whitegoldenretriever.com/available-pups/

But with the rewrite rule above, if I type in recherchgoldens.com/Available-Pups.html, it just forwards to whitegoldenretriever.com/Available-Pups.html

But I don't want that. I want it to still forward to the location set in my Redirect 301 directive.

What am I doing wrong?

1 个答案:

答案 0 :(得分:6)

仅重定向主页使用:

RewriteCond %{HTTP_HOST} ^recherchegoldens\.com$ [NC]
RewriteRule ^/?$ http://whitegoldenretriever.com/ [R=301,L]

确保在新浏览器中对此进行测试,以避免旧的浏览器缓存。