how do I force the URL to include www.

Started by fwitt, October 16, 2006, 07:15:12 PM

Previous topic - Next topic

fwitt

I have a script running that only works if it is accessed via the URL
http://www.example.com/script.php
and not if it is accessed via
http://example.com/script.php
how do I check the URL and insert the www. if it is missing?

SleePy

This on SMF or a stand alone script?

If you want a stand alone script you got 2 options.
1. If you use apache and mod_rewrite is enabled you can use that to redirect.

2. Use php to detect current url and redirect. But if the script can't run at all without the www meaning the php script wont process at all this wont work..

Jeremy D ~ Site Team / SMF Developer ~ GitHub Profile ~ Join us on IRC @ Libera.chat/#smf ~ Support the SMF Support team!

fwitt

its a script thats calling SSI functions, if the www. isnt in the url no info is returned by some of the SSI functions the rest of the php script works fine with or without the www.

option 2 is the one I'm aiming for but I cant find the php function the returns the current url, I've probly overlooked the obvious here.

fwitt

I tried $_SERVER['SERVER_NAME'] but this returns the www. whether it is in the URL or not.

fwitt

found it $_SERVER['HTTP_HOST'] seems to do what i want.

now to write the script...

BoaR


Remaker

Quote from: fwitt on October 17, 2006, 06:20:40 AM
its a script thats calling SSI functions, if the www. isnt in the url no info is returned by some of the SSI functions the rest of the php script works fine with or without the www.

You could fix this just by enable subdomain independent cookies in the administration center , as I am prety sure you've got cookie problems ;)
Remaker - My blog

fwitt

Quote from: BoaR on October 26, 2006, 02:00:43 PM
please share script.. i need this too.

this is the code i put at the top of the php file to force, the www. to appear.



//lets force the URL to include www. (some SSI functions dont work without it)
if ($_SERVER['SERVER_NAME'] !== $_SERVER['HTTP_HOST'])
{
  header('Location: http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING']);
  exit;
}



for a file accessed at http://example.com/index.php?action=test this redirects to http://www.example.com/index.php?action=test it does not include the file path if the file is not in your domains root, but that can be added between $_SERVER['SERVER_NAME'] and $_SERVER['PHP_SELF'] if needed.

Quote from: Remaker on October 26, 2006, 02:46:59 PM
You could fix this just by enable subdomain independent cookies in the administration center , as I am prety sure you've got cookie problems ;)

I will have to play with that on my test site, it definatly would have been an easier solution, but I learnt stuff by doing it the other way and thats always good.

BoaR

there's something wrong with the php code, it adds " index.php? " when first visiting the website at the end of the web address.. removing $_SERVER['QUERY_STRING'] should fix this problem but i dont know what this will cause to other links, i wont be using the code until i find a better code for this, i think taken the nuke code for this will do the trick, i'll post back if it works.

fwitt

o thats easy to fix



//lets force the URL to include www. (some SSI functions dont work without it)
if ($_SERVER['SERVER_NAME'] !== $_SERVER['HTTP_HOST'])
{
  $target = 'Location: http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
  if (!empty($_SERVER['QUERY_STRING']))
    $target = $target . $_SERVER['QUERY_STRING'];
  header($target);
  exit;
}

kegobeer

This is done in an .htaccess file using a rewriterule.
"The truth of the matter is that you always know the right thing to do. The hard part is doing it." - Norman Schwarzkopf
Posting and you (Click "WATCH THIS MOVIE")

Daniel15

Yep, the best way to go about this is via a mod_rewrite rule in .htaccess. Do something like:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.dansoftaustralia.net$ [NC]
RewriteRule ^(.+)$ http://www.dansoftaustralia.net/$1 [R=permanent]

Replace www.dansoftaustralia.net with the correct domain, of course. This will redirect all access to the www.[domain] domain, using a 301 (permanent) redirect
Daniel15, former Customisation team member, resigned due to lack of time. I still love everyone here :D.
Go to smfshop.com for SMFshop support, do NOT email or PM me!

kegobeer

I've never seen [R=permanent], only [R].  Although it may work, if you chain options I don't think =permanent is permitted.
"The truth of the matter is that you always know the right thing to do. The hard part is doing it." - Norman Schwarzkopf
Posting and you (Click "WATCH THIS MOVIE")

Daniel15

Yeah, [R=permanent] works (as well as [R=301])
See the Apache documentation (http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule)
Quote'redirect|R [=code]' (force redirect)
Prefix Substitution with http://thishost[:thisport]/ (which makes the new URL a URI) to force a external redirection. If no code is given, a HTTP response of 302 (MOVED TEMPORARILY) will be returned. If you want to use other response codes in the range 300-400, simply specify the appropriate number or use one of the following symbolic names: temp (default), permanent, seeother.
Daniel15, former Customisation team member, resigned due to lack of time. I still love everyone here :D.
Go to smfshop.com for SMFshop support, do NOT email or PM me!

Advertisement: