Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I make index.php add" www" to http://site.com when page loads?

Status
Not open for further replies.

zoldos

Technical User
Nov 5, 2008
90
US
I have a site that gets confused if someone logs in WITHOUT the "www" in their URL. Is there a way for index.php to automatically add the " This would be a huge help!

Thanks!
 
in order of best solution (with the first being best); you would be better:

1. recoding your site to be less touchy about www. if you explain the issue then we may be able to help. Is it to do with login's and remembering logged in users perhaps?

2. using apache mod_rewrite to do this task.

but if you must use php, this should be ok (warning - not tested for syntax errors)

Code:
$pattern = "/^(http:\/\/)?[URL unfurl="true"]www.(.*?)$/i";[/URL]
if(1 == preg_match($pattern, $_SERVER['REQUEST_URI'], $matches)):
  header ('HTTP/1.1 301 Moved Permanently');
  header ('Location: [URL unfurl="true"]http://www.'[/URL] . $matches[2]);
  die();
endif;

you could install it as a prepended file. using an .htaccess file to prepend only for that (virtual) host. or install as an ordinary file and use require_once to bring the code in at every entry point that your web app supports. For my style of coding (often known as despatch) then you would need to amend only one file.
 
In more detail, if someone logs into my site using and then posts a picture, or a series of pictures, then another user who logged in via tries to view the pics, it will not realize they are logged in and produce an error. I've not tested it on other functions, but pictures posted is the main thing done on my site so naturally this is an issue.

I don't HAVE to use php, but that is what my site is written in (I didn't write it, I'm using a script I purchased). I'd prefer to use .htaccess if you can explain that for me! :) Or the require_once in index.php might work too. I'm still a newbie to PHP so more information on how to do that would be appreciated! Thanks!
 
the solution posted will work.

however the 'real' issue appears to be that the cookie domain for sessions is set incorrectly at a subdomain rather than root domain level. or not set at all, in which case it will inherit from the host name of the server.

edit the relevant php.ini and change the session.cookie-domain entry to point to ".domain.com" (the prepended dot is important) and not
for the htaccess solution, this assumes you are using apache and you have mod_rewrite installed and enabled. if you don't have this prerequisite then head over to the apache forum and ask there.

this is the rule for the rewrite that should go into the htaccess file. the file should be named .htaccess (again, the prepended dot is important) and should be placed in the domain root.

Code:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^[URL unfurl="true"]www\.[/URL] [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://[URL unfurl="true"]www.%{HTTP_HOST}%{REQUEST_URI}[/URL] [R=301,L]
 
The first method didn't work, but the second one works great! Thanks so much!!
 
for future readers it would help if you explained what did not work about the first solution. what error messages were received? what symptoms of 'not working' were exhibited etc?
 
ah - in case you meant the changing of the cookie parameter as the 'first option', this will work fine provided that you reboot the server (or at least restart the apache server) after making the change to php.ini. however it will not work if you use the same server externally and as localhost.

remember - any changes to php.ini when running php as a sapi will only be in place once the apache server has been restarted.
 
Hi

zoldos said:
The first method didn't work, but the second one works great! Thanks so much!!
Surprising little mistake from Justin.
php.net said:
'REQUEST_URI'
[ ] [ ] The URI which was given in order to access this page; for instance, '/index.html'.

[tt]$_SERVER['HTTP_HOST'][/tt] should be tested instead. Somehow like this :
PHP:
[b]if[/b] [teal]([/teal][COLOR=darkgoldenrod]substr[/color][teal]([/teal][navy]$_SERVER[/navy][teal][[/teal][green][i]'HTTP_HOST'[/i][/green][teal]],[/teal] [purple]0[/purple][teal],[/teal] [purple]4[/purple][teal])[/teal] [teal]!=[/teal] [green][i]'www.'[/i][/green][teal])):[/teal]
  [COLOR=darkgoldenrod]header[/color][teal]([/teal][green][i]'HTTP/1.1 301 Moved Permanently'[/i][/green][teal]);[/teal]
  [COLOR=darkgoldenrod]header[/color][teal]([/teal][green][i]'Location: [URL unfurl="true"]http://www.'[/URL][/i][/green] [teal].[/teal] [navy]$_SERVER[/navy][teal][[/teal][green][i]'HTTP_HOST'[/i][/green][teal]][/teal] [teal].[/teal] [navy]$_SERVER[/navy][teal][[/teal][green][i]'REQUEST_URI'[/i][/green][teal]]);[/teal]
  [b]die[/b][teal]();[/teal]
[b]endif[/b][teal];[/teal]
[small][maroon]Warning[/maroon] The above code was not tested[/small]

Feherke.
[link feherke.github.com/][/url]
 
Hi feher

i may have made a mistake but it was intentional. given that this would only happen on a first visit to a domain I took the view that the request URI would never be relative, but always a URL. and when it were a FQDN the whole string (HOST + SERVER NAME + SCRIPT + QUERY STRING) would be available in that superglobal.

is that incorrect?

 
Hi

Sorry, I am not able to follow the explanation at this hour. But personally I never saw hostname in the [tt]REQUEST_URI[/tt] and apparently neither the documentation says it may contain. So looking for subdomain in the [tt]REQUEST_URI[/tt] sounds wrong.

Feherke.
[link feherke.github.com/][/url]
 
It simply didn't work. I placed the first option's code at the top of my index.php file. When the page loaded, everything looked the same and it did not forward me to There were no error messages...
 
Feher - I think you are right, in the cold light of morning. I was misled into thinking that the word REQUEST_URI actually meant a URI. I was concerned about using the HOST value as proxies play around with this. But I guess it's all there is unless the OP maintains two different sites or changes the cookie domain (which would be the norm).

@Zoldos - Feher's code above should work. either put it at the top of your receiving script or in a separate file that is then 'required' by the receiving script. Or, as said, add the file as an auto_prepend_file in .htaccess or a local php.ini ( (there are other alternatives for including the file depending on what version of php you are using. but .htaccess is the norm. e.g.
Code:
<IfModule mod_php5.c>
php_value auto_prepend_file "path/to/file.php"
</IfModule>
[/code[
 
The problem is already fixed. I appreciate your time. [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top