Setting up subdomain

Discussion in 'Site Programming, Development and Design' started by Ayrehead, Oct 21, 2017.

  1. I'm trying to redirect a subdomain to a file and I'm doing the way it says in:
    https://support.winhost.com/kb/a649/how-to-redirect-a-subdomain-to-a-subdirectory.aspx

    The article says to put some code in default document on your document root.

    I think my problem is I already have a default file: index.php. It launches joomla. I don't want to disturb that file.

    Is there a way to put the redirecting code in some other default document on the document root? Such as Default.asp? Maybe tech support can change <defaultDocument> element to list another default doc before index.php?
     
  2. Elshadriel

    Elshadriel Winhost Staff

    Michael likes this.
  3. I already have a default document, index.php. That still needs to execute for visitors to the main domain. Since there can be just 1 default document that executes, that one will execute for visitors to the new subdomain too. Therefore the code for redirect must be put into index.php.

    I tried putting in the script from the knowledgebase into index.php:
    <%@ Language="VBScript" %>
    <%
    If InStr( UCase(Request.ServerVariables("SERVER_NAME")), UCase("m.mydomain.org") ) > 0 Then Response.Redirect("/test.txt")
    %>
    <?php
    ...

    But that doesn't work, doesn't execute. Can anyone tell me the php code to put in there instead?
     
  4. Elshadriel

    Elshadriel Winhost Staff

    I don't have the php code offhand, but the reason it doesn't work is because it is VBScript which uses a different extension. You can rename the file to index.asp to execute the script. It's just a redirect script. It won't affect the rest of your php code.
     
  5. I can't rename the file to index.asp because the file already exists and contains php code that launches joomla for the main site. The file must remain a php file. I am not familiar with php so that's why i at least tried to paste the VBS code in to see if it would work. We now know it doesn't. So i need php code to put in the php file to redirect the subdomain.
     
  6. Elshadriel

    Elshadriel Winhost Staff

  7. Those don't tell how to make php code for the logic:

    If InStr( UCase(Request.ServerVariables("SERVER_NAME")), UCase("subdomain3.HostingAccountDomain.com") ) > 0 Then
    Response.Redirect("/subdomain3/home.asp")
     
  8. The majority of people running PHP are doing so on linux servers, so redirects like this are better handled via .htaccess. That's why you don't see a ton of sample code out there for doing an in-page redirect with PHP.

    That being said, you might try this at the top of the PHP index page:

    <?php
    $host = "subdomain.domain.com";

    if ($_SERVER['HTTP_HOST'] == $host) {
    header("Status: 301 Moved Permanently");
    header("Location: http://domain.com/subdomain/page.xxx");
    exit;
    } else {
    <your existing PHP code for the non-subdomain content of the index page>
    }
    ?>

    I haven't tested it anywhere, but it should work.

    Note that the "exit;" line will prevent any following code from executing. But since only the subdomain host would trigger the exit code, it shouldn't be a problem.

    There's also meta http-equiv="refresh", but that can't detect whether the request is for a subdomain, so it doesn't work here. There's javascript out there that will redirect a page too. You might be able to search that out if the PHP doesn't work for your application.
     
    Elshadriel likes this.
  9. Thanks for your help. Your suggestion works. Case closed.

    For the benefit of someone else searching for a solution, here is a recap.

    I created a subdomain mobile.mydomain.org.
    When someone surfs to that, the system runs the default document at my site's root.
    My site already has a default doc index.php that runs when someone surfs to mydomain.org.
    I don't want to mess with that index.php.
    I put the new php code in a new file default.php.
    It detects when a visitor requests the subdomain and then redirects.
    If a visitor does not request the subdomain, the code executes regular index.php.
    I used IIS Manager to move default.php ahead of index.php.

    The code:

    <?php
    $host = "mobile.mydomain.org";
    if (($_SERVER['HTTP_HOST'] == $host) or ($_SERVER['HTTP_HOST'] == strtoupper($host))) {
    header("Status: 301 Moved Permanently");
    header("Location: http://mydomain.org/mobile-home-page");
    exit;
    } else {
    include('index.php');
    }
    ?>
     
    Elshadriel and Michael like this.
  10. Great, I'm glad the problem was solved.

    The uppercase string conversion covers all the bases, good addition.
     
    Elshadriel likes this.

Share This Page