TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   http://domain.com => http://www.domain.com (http://www.talkphp.com/general/1810-http-domain-com-http-www-domain-com.html)

exorcist 12-29-2007 01:46 PM

http://domain.com => http://www.domain.com
 
When i open my page without www, like Domain.com in IE, images dont display, only when i type it with www infront...

So i try to fix that with this code, but no luck, any help?, i dont have .htaccess support?

Quote:

<?PHP
if ( substr ( $_SERVER['SERVER_NAME'], 0, 4 ) != 'www.' )
{
header ( 'location: http://www.' . $_SERVER['SERVER_NAME'] );
exit ();
}
?>

Wildhoney 12-29-2007 02:52 PM

You're best off doing this with HTAccess, and resultantly a 301 redirect for SEO purposes. This can be achieved like so:

Quote:

Originally Posted by HTAccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourhost.com [NC]
RewriteRule ^(.*)$ http://www.yourhost.com/$1 [L,R=301]


exorcist 12-29-2007 03:23 PM

I said server dont support .htaccess ;)

Aaron 12-29-2007 04:03 PM

Thats impossible O.o are you using apache?

Wildhoney 12-29-2007 05:03 PM

In that case then, the code you published is working perfectly fine. Unless your host has disabled htaccess on an individual basis, and your web server is running Apache, then you will have htaccess support. I don't know of any hosts these days that disable htaccess. Try getting in touch with them.

deflated 12-29-2007 05:06 PM

Quote:

Originally Posted by exorcist (Post 7256)
When i open my page without www, like Domain.com in IE, images dont display, only when i type it with www infront...

Are you suppressing warnings? Perhaps your header() has just failed. Could you copy the following code into your file directly before the if() construct and post the response here?

PHP Code:

//...

//firstly we check if there's already been output
if (headers_sent($file$line)) {
  
//okay, that was simple: the problem is that header() failed
  //now find out where the first output has been made
  
echo 'Headers already sent in "' $file '" in line "' $line '"' PHP_EOL;
} else {
  
//substr ( $_SERVER['SERVER_NAME'], 0, 4 ) seems to be "www."
  
var_dump $_SERVER['SERVER_NAME'] );
  echo 
PHP_EOL;
  
var_dump substr $_SERVER['SERVER_NAME'], 0) != 'www.' ) );
  
print_r $_SERVER );
}

die();

//... 

You could also try to replace "location" by "Location" so that it looks like:
PHP Code:

header('Location: http://www.example.com/'); 

Perhaps that's been an Internet Explorer specific problem. Have you already other browsers like the Mozilla Firefox or Opera?

Why do you redirect at all? Even better is to use a function which returns the URL to your script without the scriptname itself (You could also use a variable but functions can be used in different parts of your code - okay you could use the global/static keyword for that but I prefer functions or Singleton classes). So when you want to use an image just add getUrl() before the filename.


All times are GMT. The time now is 01:24 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0