Simple Machines Community Forum

General Community => Scripting Help => Topic started by: fwitt on October 16, 2006, 07:15:12 PM

Title: how do I force the URL to include www.
Post by: fwitt on October 16, 2006, 07:15:12 PM
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?
Title: Re: how do I force the URL to include www.
Post by: SleePy on October 16, 2006, 09:35:06 PM
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..

Title: Re: how do I force the URL to include www.
Post by: 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.

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.
Title: Re: how do I force the URL to include www.
Post by: fwitt on October 17, 2006, 06:58:01 AM
I tried $_SERVER['SERVER_NAME'] but this returns the www. whether it is in the URL or not.
Title: Re: how do I force the URL to include www.
Post by: fwitt on October 17, 2006, 07:41:49 AM
found it $_SERVER['HTTP_HOST'] seems to do what i want.

now to write the script...
Title: Re: how do I force the URL to include www.
Post by: BoaR on October 26, 2006, 02:00:43 PM
please share script.. i need this too.
Title: Re: how do I force the URL to include www.
Post by: Remaker on October 26, 2006, 02:46:59 PM
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 ;)
Title: Re: how do I force the URL to include www.
Post by: fwitt on October 26, 2006, 03:45:11 PM
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.
Title: Re: how do I force the URL to include www.
Post by: BoaR on October 26, 2006, 10:22:09 PM
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.
Title: Re: how do I force the URL to include www.
Post by: fwitt on October 27, 2006, 06:39:59 AM
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;
}
Title: Re: how do I force the URL to include www.
Post by: kegobeer on October 27, 2006, 07:06:14 AM
This is done in an .htaccess file using a rewriterule.
Title: Re: how do I force the URL to include www.
Post by: Daniel15 on October 27, 2006, 11:06:35 AM
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
Title: Re: how do I force the URL to include www.
Post by: kegobeer on October 27, 2006, 02:44:25 PM
I've never seen [R=permanent], only [R].  Although it may work, if you chain options I don't think =permanent is permitted.
Title: Re: how do I force the URL to include www.
Post by: Daniel15 on October 27, 2006, 10:11:48 PM
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.