View Full Version : Weird problem with header("Location: ...")

01-07-2008, 07:58 PM
I'm experiencing a weird and very frustrating problem. I am trying to run our page via the www. prefix instead of dropping the prefix as our page seems to run better while using the prefix.

When I try to use

header("Location: http://www.mydomain.com/path/to/file/index.html")

it indeed redirects me to this page, however, it redirects me to


instead of


Why would this happen? I thought it maybe had something to do with the quotes, so I've tried single and double quotes both with the same results.

Any ideas?

01-07-2008, 08:18 PM
Your hosting company probably has a mod rewrite for all domains. Does the www stick around if you just type it into the browser?

01-08-2008, 05:34 AM
yes, when i type it into the browser, it goes to the www prefix just not when I use the header function in php.

I'm going to test this as a stand alone program (one liner) and see what I come up with.

01-08-2008, 05:47 PM
Alright, so I figured it out after using a plug-in for firefox called live http headers was able to determine that the header was being sent back as HTTP Error 301 - Moved permanently and it therefore tried the domain without the www prefix.

This was fixed only when I changed the Location header from this

header("Location: http://www.ourdomain.com/path/to/dir")

to this

header("Location: http://www.ourdomain.com/path/to/dir/index.php")

notice the index.php at the end of the path... For some reason it will not redirect to a directory through the www prefix.

Hope this helps someone else as well,