Using mod_rewrite to redirect home page ONLY

泄露秘密 提交于 2019-12-22 09:25:59

问题


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:


To redirect only home page use:

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

Make sure to test this in a new browser to avoid old browser cache.



来源:https://stackoverflow.com/questions/30038533/using-mod-rewrite-to-redirect-home-page-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!