SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Changing domain name after a re-direct

    Hi,

    I have just completed a redesign of a web site that was hosted on another server (Solaris). The old webmaster has created a test domain name in his DNS to send traffic from his server to my server (apache). I have a PHP script on my server that looks at all incoming traffic and if the request is coming from this test domain name, then it redirects the visitor to the new design. Here is the code for that:

    PHP Code:
    if ($_SERVER['HTTP_HOST'] == "webtest.abc.com") {
            
    header('HTTP/1.1 301 Moved Permanently');
            
    header('Location: http://xyz.net/~imperial/');
            exit();
        }
        
    ?> 
    This works fine, except that the domain name that is displayed in the browser location bar is xyz.net/~imperial and I would like it to say imperialconstruction.com. Is this possible?

    Thank you in advance,
    Mark
    Last edited by mjbeck; Apr 8, 2005 at 08:17.

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I figured it out.

    I changed the line in the PHP script to:

    header('Location: /~imperial/');

    Mark

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,650
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Mark,

    Actually, this should still give you the ~imperial directory.

    If you use httpd.conf to set the xyz.net (imperialconstruction.com) DocumentRoot to the ~imperial directory, you'll have it made without showing ~imperial in the URL.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much for the reply!

    I'm on a shared server, so is there a way to do this through .htaccess?

    Thanks,
    Mark

  5. #5
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,650
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Mark,

    Well, it would be a repeatitive burden on the server to do that with .htaccess -- and it should have been set-up properly to begin with -- that the httpd.conf is the way to go.

    That said, mod_rewrite is extremely powerful so, yes, it can be done.

    .htaccess (xyz.net root directory):
    Code:
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^~imperial/
    RewriteRule $(.*)$ ~imperial/$1 [QSA,L]
    This will look at the requested URL at xyz.net and, if it is not asking for something from the ~imperial directory, it will redirect ALL requests to the ~imperial directory AND append any query string that had been in the request.

    However, I still believe that it would be best to get the site set-up properly in the httpd.conf. That's replacing the line that says something like ...

    DocumentRoot C:/apache/Apache2/htdocs/imperialconstruction

    with

    DocumentRoot C:/apache/Apache2/htdocs/imperialconstruction/~imperial

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks David, I appreciate your help.

    1) If I'm able to convince my hosting company to make the change in the config file, will my PHP code from above still work as it is written?

    2) I tried your mod_rewite and although the domain name in the browser's location bar was correct, my images and stylesheet were not being loaded. I link to the stylesheet as follows:

    Code:
    <link rel="stylesheet" href="home.css" type="text/css">
    What would I have to change this to to make it work?

    Sorry for the dumb questions, but I'm a noobie when it comes to htaccess rules.

    Thanks,
    Mark

  7. #7
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,650
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Mark,

    1) If I'm able to convince my hosting company to make the change in the config file, will my PHP code from above still work as it is written?

    No. In fact, you won't need it at all.

    2) I tried your mod_rewite and although the domain name in the browser's location bar was correct, my images and stylesheet were not being loaded. I link to the stylesheet as follows:

    What would I have to change this to to make it work?

    That was covered in the WARNING in the article at datakoncepts.com/seo but the short answer is to use the HTML base tag to let the script(s) know where they are (the redirection confuses Apache and the browsers) so your relative links will continue to work properly.

    Sorry for the dumb questions, but I'm a noobie when it comes to htaccess rules.

    No worries, mate! NO question is dumb if you don't know the answer. Besides, we were ALL noobies at one time or another. That's called learning and that's what we are ALL doing in these forums. If we've accomplished that, I'll call it a success!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi David,

    We are getting closer!

    I've implemented the mod_rewrite with the base tag as you suggested.

    PHP Code:
    <base href="http://<?php echo $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']; ?>" />
    When you go to http://webtest.imperialconstruction.com/ everything works great. But when you click on a link, the dreaded ~imperial comes back in the location bar.

    Thanks,
    Mark

  9. #9
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,650
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Mark,

    Hmmm, that's EXACTLY what you'd expect using SCRIPT_NAME (but KUDOS to you for using that under PHP!).

    Now, are your links referenced to the ~imperial directory or to your root? For your use (where (1) you're going to get your hosting provider to get their act together and get your DocumentRoot setup correctly or (2) you won't need to correct mod_rewrite's corrections), change your base script to read:

    <base href="http://
    <?php
    $filename = pathinfo($_SERVER['SCRIPT_NAME']);
    echo $_SERVER['HTTP_HOST'] . filename['basename'];
    ?>
    " />

    That should cut-out the ~imperial/ part of the path which is what you're looking for, isn't it? Then, until your host provides you some service, ALL your requests will be redirected to ~imperial/{REQUEST_URI} where all your files are. After you switch your host provider, it'll work without the redirect.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  10. #10
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi David,

    I asked my hosting company what the Document setting was in httpd.conf and they replied with: /home/imperial/public_html. I'm thinking that wasn't the answer you would have expected. Should I have asked it differently?

    As for the most recent base tag code, I ran into a problem. First there should have been a $ in front of the filename variable (which I added). When I uploaded the new code, and mouse over the links, they look like this:

    imperialconstruction.comindex.php/index.php?whoweare

    I have removed the code and went back to the original with the ~imperial in the URLS.

    I tried different variations but just can't seem to get it to work. When the links look right, I'm losing the images and stylesheet, when the page looks good, the links don't work.

    Thanks again for all your help, I really appreciate it.

    Mark

  11. #11
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,650
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Mark,

    Indeed! It sounds like you've introduced the ~imperial directory yourself (under the public_html directory which is the DocumentRoot of your webspace). That said, you must've asked the question correctly as that's an important answer.

    My apologies for the PHP code -- I should test these things rather than rush through:

    <base href="http://
    <?php echo $_SERVER['HTTP_HOST'] . '/' . basename($_SERVER['SCRIPT_NAME']); ?>" />

    Admittedly, I wasn't expecting the query string to be attached. However, it looks like you'd left in some old code 'cause the basename part of pathinfo is just the filename and does not include the query string. I've changed the code I gave 'cause you don't need to deal with parsing the file's path/name when you use the basename function.

    When you lose relative links (css, images, scripts), that means that your redirects are working and Apache thinks the file is being served from the new location (and the base tag is not working properly - it's probably confused, too, so that an ABSOLUTE URL is required rather than PHP).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •