Forum Moderators: phranque

Message Too Old, No Replies

localhost redirecting to live server

         

johnblack

8:57 am on Oct 9, 2006 (gmt 0)



Hi,

Whenever I browse to [localhost...] I end up at http://www.example.com/ where www.example.com is the url of the live website. I must have set something up incorrectly in the .htaccess file or may be have Apache set up incorrectly locally.

Anybody have any ideas as to what setting could cause this?

Cheers John

jdMorgan

1:20 pm on Oct 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It could be any number of things. First, look through your config and .htaccess files for any mention of your actual domain name.

In some cases, you can create 'portable' configuration code by using server variables such as %{HTTP_HOST} in your code, instead of hard-coding the domain name.

Jim

johnblack

7:09 am on Oct 10, 2006 (gmt 0)



Thanks for your reply Jim. The site is not mentioned in the htaccess nor the config file.

In the .htaccess file I have a line

RewriteCond %{HTTP_HOST}!^www\.example\.com

Would that be redirecting localhost to the live server?

jdMorgan

1:12 pm on Oct 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, combined with a following rewriterule, that might do it.

That line says, "If requested host is NOT www.example.com" and the most likely following rule would say, "then redirect to www.example.com."

In order to avoid having two versions of the code, one for testing and one for use on the live site, you can simply 'allow' the use of localhost without redirecting. Something like:


RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} !^localhost
RewriteCond %{HTTP_HOST} !^127\.0\.0\.1
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Essentially, this rule always redirects requests to www.example.com, but is disabled by one of the RewriteConds if the requested host is already www.example.com, or if it is "localhost" or the local host IP address 127.0.0.1

Jim