domain www and without www

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

domain www and without www

Post by nite4000 »

Hey everyone I am hoping someone can tell me how I can do this I have seen the code somewhere but not sure where.

I have a script that calls home and check to be sure the domain the script is on is in the database but the solution I want to do it instead of me having to have 2 entries in my table for the same domains just have one

example

say if www.mysitename.com is using my script

in the db i have to enter 1 record as mysitename.com and then insert another as www.mysitename.com

otherwise if a user go to their site and enters one of these and its not in the table it will say its illegal copy. I want to have it where I can just have www.mysitename.com and thats it that way if a user goes to mysitename.com the site will still show up but not redirect to www but just come up at either address.

Hope someone can help
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: domain www and without www

Post by flying_circus »

Seems like a job for mod_rewrite

This script looks for strings that dont match http://www.your_domain.com. If you have multiple domains, you might want to modify it to look only for the "www." part.

[syntax]RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.your_domain.com$
RewriteRule ^(.*)$ http://www.your_domain.com/$1 [R=301][/syntax]
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: domain www and without www

Post by twinedev »

Add to .htaccess (or even better your vhost entry):

Code: Select all

<IfModule mod_rewrite.c>
     RewriteEngine on
     RewriteCond %{HTTP_HOST} !^www.mywebsite.com$ [NC]
     RewriteRule ^(.*)$ http://www.mywebsite.com$1 [R=301,L]
</IfModule>
This way if they hit http://mywebsite.com it will auto send them over to http://www.mywebsite.com/

-Greg
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: domain www and without www

Post by nite4000 »

I dont want to redirect.

In my licese table right now I have to input their site as www.domain.com and then input it again as domain.com

I am trying to make it where i can just enter the www.domain.com and no matter what a user enters whether it be http://www.domain.com OR domain.com, OR www.domain.com or http://domain.com

the url will bring up the site but will accept it and see the domain name is in the table.

I could not be explaining it right I have seen the code somewhere I wanna say it was preg_match or something.


Sorry for any confusion
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: domain www and without www

Post by flying_circus »

Aha, I definately misunderstood your initial request, I think I tried to read it to fast and didnt catch all the details.

The first option that comes to mind is using str_replace.

something like:

Code: Select all

  $client_request = "http://www.example.org";
  $bits_to_strip = array("http", "https", "://", "www.");

  $stripped_request = str_replace($bits_to_strip, "", $client_request); //example.org
But this isn't fool-proof. Consider "http://shop.example.org".

I think the better option is to use a regular expression, but I am no where near proficient enough to solve that problem right now. Hopefully someone can chime in, or you might get more traction in the regular expressions forum.
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: domain www and without www

Post by nite4000 »

it dont matter about subdomains as long its the right domainname.com or what ever the extension.

I am just trynt to elimate having to enter 2 entries in my table and have it chk for the domain name ( name.com) no matter what the beginning is rather it be http:// or https:// or www and so on


So would that code be the best bet to achieve this?

Thanks
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: domain www and without www

Post by mikeashfield »

Code: Select all

<?php
  $txt='http://www.google.co.uk/';
  $re1='((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))';
  if ($c=preg_match_all ("/".$re1."/is", $txt, $matches))
  {
      $httpurl1=$matches[1][0];
      print "($httpurl1) \n";
  }
?>
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: domain www and without www

Post by nite4000 »

Thanks everyone for your help however i need assistance with making this work. let me show you how I currently chk for the domain then maybe someone can tell me how I can intergrate this new code

this is what i have

Code: Select all


$domain1 = $_SERVER['HTTP_HOST'];

$q = mysql_query("SELECT * FROM tbllicense WHERE domain = '{$domain1}'",$link2) or die(mysql_error());
$valid = mysql_fetch_array($q, MYSQL_ASSOC);
@mysql_free_result($q);

now on the pages of my script i have this

Code: Select all

if($valid['domain'] != $domain1)
{
//give error
}


now to explain somethings. the $link you see on the query is the connection to the right database since this connects to the main site and not the users site for checking the license.

i just need help with where and what to put either on the include page where the first section of code is or on the script pages where the 2nd section of code is.

i am thinking i would add it on the page with the quesry but i wanna be sure and i dont know how
Post Reply