TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-29-2007, 01:46 PM   #1 (permalink)
The Visitor
Newcomer 
 
Join Date: Dec 2007
Posts: 3
Thanks: 0
exorcist is on a distinguished road
Help 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 ();
}
?>
exorcist is offline  
Reply With Quote
Old 12-29-2007, 02:52 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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]
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-29-2007, 03:23 PM   #3 (permalink)
The Visitor
Newcomer 
 
Join Date: Dec 2007
Posts: 3
Thanks: 0
exorcist is on a distinguished road
Default

I said server dont support .htaccess ;)
exorcist is offline  
Reply With Quote
Old 12-29-2007, 04:03 PM   #4 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

Thats impossible O.o are you using apache?
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 12-29-2007, 05:03 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-29-2007, 05:06 PM   #6 (permalink)
The Wanderer
 
deflated's Avatar
 
Join Date: Dec 2007
Location: 127.0.0.1
Posts: 19
Thanks: 7
deflated is on a distinguished road
Default

Quote:
Originally Posted by exorcist View Post
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.

Last edited by deflated : 07-18-2010 at 01:46 PM.
deflated is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:29 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design