Need help running website on specific domain

Discussion in 'Site & Server Administration' started by StevePro, May 7, 2009.

  1. #1
    Hi All,

    I'm not sure if this is a DNS question or a Server question so sorry if it's posted in the wrong place.

    I have a website that currently has 3 domain names pointed to it (domain1.net, domain2.net, domain3.net) and want to host the site on only 1 of these domains - e.g. site is hosted on domain2.net, if someone goes to domain1.net the url will change/forward to domain2.net

    My idea was to use a cname so, for example, www.domain1.net would have a cname pointing to www.domain2.net - however this hasn't worked.

    Any advice greatly appreciated.
     
    StevePro, May 7, 2009 IP
  2. relixx

    relixx Active Member

    Messages:
    946
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    70
    #2
    Ok, if domain2 is the "correct" one, and domain1 and domain3 are cnames, you still need to configure the webserver to do the redirects, cnames only work at the DNS-level

    1 method is to have the other 2 domains set up as site aliases in domain2's config file, and then use a 301 redirect to redirect all traffic from domains 1 and 3 to domain2 :) This works because when you request domain1 or 3, it'll server up domain2's content (and thus see the redirect rule)

    Another method assumes that each domain has its own config file on the webserver, in which case you can simply set up 301 redirects in those two config files to redirect traffic to domain2
     
    relixx, May 7, 2009 IP
  3. dalem

    dalem Peon

    Messages:
    494
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You could also use the Virtual Host container found in apache.
    Look in Apache ->> Conf ->> Extra->> httpd.vhosts.conf
    I've used this method with great success.
     
    dalem, May 7, 2009 IP
  4. StevePro

    StevePro Peon

    Messages:
    106
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi,

    The server is set up to serve the same site to each domain, I've given more detailed info below:-

    The site I was developing is www.suffolkpine.co.uk which is a pine and oak supplier, Google have picked up on the domain name and registered it in it's index - however the client want the domain www.suffolkpineandoakshowroom.co.uk to be the one indexed by Google. So what I am trying to do is redirect the url www.suffolkpine.co.uk to www.suffolkpineandoakshowroom.co.uk so that visitors are shown the longer url when visiting www.suffolkpine.co.uk and Google only indexes www.suffolkpineandoakshowroom.co.uk.

    Currently the web server is using alieses to show the same site on the different url's.

    I have for the minute created an index.html page with a meta refresh to send all customers (and search engines) to www.suffolkpineandoakshowroom.co.uk/index.php - however this is ugly and if there is a better way of doing this I'd like to know.

    I hope this explains the situation, again any advice is welcome.
     
    StevePro, May 11, 2009 IP
  5. relixx

    relixx Active Member

    Messages:
    946
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    70
    #5
    Hi Steve,

    Ok, looks like a straight-forward 301 redirect. As the sites are aliases, all you need to do is put the 301 redirect commands in the the .htaccess file (I see that you are already using mod_rewrite on the URLs).

    Meta refreshes are ugly and look spammy to Google, which will lead to penalties.

    In the .htaccess file, put in the following directives:

    
    RewriteEngine On [this only needs to appear once in the file...]
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^www\.suffolkpine\.co\.uk [NC]
    RewriteRule ^(.*) http://www.suffolkpineandoakshowroom.co.uk/$1 [R=301,L]
    
    Code (markup):
    This will cause any traffic to the short url to be redirected to the long one.

    As it's a 301 redirect Google will then stop indexing the short url and start treating the long one as the proper one. This is the correct way to do it, to try have visitors be directed to one domain but google another is dodgy and will lead to penalties
     
    relixx, May 11, 2009 IP
  6. StevePro

    StevePro Peon

    Messages:
    106
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for the reply, however I have a bit of a unique issue here it seems. The site www.suffolkpine.co.uk is hosted on a plesk server and stores all the file for the site. I really don't want to move these files to another directory as a lot of permission changes have taken place as well as a lot of chmods. The domain name www.suffolkpineandoakshowroom.co.uk is an alias of suffolkpine.co.uk and uses the same directory tree - therfore if I try to use the .htaccess route I end up in an infinat loop.

    It seems I am unable to change the suffolkpine.co.uk domain name within Plesk so am stuck with it as the 'master' domain for the site.

    Can I write some php code that checks the url, and if it's not suffolkpineandoakshowroom.co.uk it then redirects to that domain name?
     
    StevePro, May 11, 2009 IP
  7. StevePro

    StevePro Peon

    Messages:
    106
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks for everyones input, I think I have found a solution to my problem:-

    What I've done is use a php function to check the page URL and if the URL is not from the domain I require then it's re-directed using a 301 redirect to the correct page - the code I am using is as the below.

    
    <?php
    function curPageURL() {
     $pageURL = 'http';
     if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
     $pageURL .= "://";
     if ($_SERVER["SERVER_PORT"] != "80") {
      $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
     } else {
      $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
     }
     return $pageURL;
    }
    ?>
    <?php
      if(substr(curPageURL(), 0, 36) != 'http://www.suffolkpineandoakshowroom') {// || substr($pageURL, 0, 32) != 'http://suffolkpineandoakshowroom') {
        Header( "HTTP/1.1 301 Moved Permanently" );
        Header( "Location: http://www.suffolkpineandoakshowroom.co.uk" );
      }
    ?>
    
    PHP:
    If anyone can see any issues with what I have done here please let me know.

    Thanks again
     
    StevePro, May 11, 2009 IP
  8. relixx

    relixx Active Member

    Messages:
    946
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    70
    #8
    edit: didn't see your other post.

    Anyway, the better thing to do would be to have the visitor transfered from shorturl.com/page to longurl.com/page, eg from www.suffolkpine.co.uk/product1 to www.suffolkpineandoakshowroom.co.uk/product1, instead of www.suffolkpine.co.uk/whatever to www.suffolkpineandoakshowroom.co.uk/

    It improves usability and transfers the google juice nicely instead of concentrating it all into 1 page (although I suppose it depends on the strategy you are using for that site)
     
    relixx, May 11, 2009 IP
  9. StevePro

    StevePro Peon

    Messages:
    106
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Can this be done via php as above?

    To be honest I'm not sure it's required - the short domain has only been registered for a month and was hardly touched by google so forwarding the entire domain to long domain shouldn't hurt.
     
    StevePro, May 19, 2009 IP
  10. relixx

    relixx Active Member

    Messages:
    946
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    70
    #10
    Well, yeah it can, although it'd be better to do it via the htaccess/conf file. If you want to do it via PHP you simply retrieve the URI via

    $_SERVER['REQUEST_URI'];
    PHP:
    then do a regex replace or whatever you want to to simply redirect from the page on the first domain to the page on the other one.
     
    relixx, May 19, 2009 IP
  11. StevePro

    StevePro Peon

    Messages:
    106
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thank you for your input Relixx, I'm still toying with the idea of using a .htaccess file but unfortunately my understanding of regular expressions is poor to say the least. I'm sure a .htaccess file will work, but so far it's ended up in infinate loops....

    The PHP code you provided is going to be my 'quick fix' until I can spare more time to investigate further.
     
    StevePro, May 19, 2009 IP
  12. relixx

    relixx Active Member

    Messages:
    946
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    70
    #12
    The commands for the htaccess file:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^www\.suffolkpine\.co\.uk [NC]
    RewriteRule ^(.*) http://www.suffolkpineandoakshowroom.co.uk/$1 [R=301,L]
    Code (markup):
    Place these at the top of the the .htaccess file. As there is a RewriteEngine On statement, it need not appear anywhere else in the file.

    These lines will check to see if the hostname being entered starts with "www.suffolkpine.co.uk", and if does a 301 redirect (the best kind for SEO purposes) to http://www.suffolkpineandoakshowroom.co.uk. The really important part of this is that actual page being visited won't change, just the domain name. Thus, www.suffolkpine.co.uk/wood/ will take you to www.suffolkpineandoakshowroom.co.uk/wood/, www.suffolkpine.co.uk/more-wood/ to www.suffolkpineandoakshowroom.co.uk/more-wood/ etc etc.

    the "\" before the fullstops in the RewriteCond %{HTTP_HOST} line is there to escape them so that the regex will treat them as regular fullstops - '.' has special meaning in regex statements.

    To do this with PHP, here is a quick 'n dirty script, which you can expand upon as you see fit:

    $hostname = $_SERVER['SERVER_NAME'];
    
    if ($hostname == 'www.suffolkpine.co.uk') {
      $uri = $_SERVER['REQUEST_URI'];
    
      if (isset($_SERVER['QUERY_STRING']) {
        $qstring = $_SERVER['QUERY_STRING'];
      }else{
        $qstring = '';
      }    
    
    header("Status: 301");
    header('Location: http://www.suffolkpineandoakshowroom.co.uk'.$uri.$qstring);
    exit;
    
    }
    PHP:
    This must appear before any other code, even HTML code. This is because even having an empty space/line before the opening php tags sends the headers to the browser, meaning that the header function being used won't work.

    Anyway, what the code above does is check if the hostname matches www.suffolkpine.co.uk and it if does, redirects to http://www.suffolkpineandoakshowroom.co.uk and adds the URI structure the visitor was initially looking for. The exit command makes sure that nothing else gets run after the redirect takes place.
     
    relixx, May 19, 2009 IP
  13. ravee1981

    ravee1981 Active Member

    Messages:
    712
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #13
    if you have a control panel, just create your main domain and park your other domains.

    If you don't, just edit the httpd and create new domains with path for the site where the files are put up.
     
    ravee1981, May 21, 2009 IP