Dynamic Subdomain Redirect.

Discussion in 'Apache' started by sparksflying, Oct 10, 2007.

  1. #1
    Hi all ,

    have read Nintendos excellent post on htaccess but not sure if its htaccess or php redirect im looking at.(im experimenting)

    I have domain www.domain.com
    I have 5 directorys inside. So www.domain.com/dir1 ( for example)
    I have ( example ) 5 subdomains on a domain to match dirs ( so dir1.domain.com)
    Im looking to redirect dir1.domain.com to www.domain.com/dir1 but still preserving the domain dir1.domain.com.
    I know I could setup hosting to point dir1.domain.com to /public_html/dir1.

    *But*
    All domains point to /public_html
    These arent actually directorys , they are rewritten categorys.

    Any ideas :)
     
    sparksflying, Oct 10, 2007 IP
  2. Ladadadada

    Ladadadada Peon

    Messages:
    382
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can solve this with either PHP or RewriteRules.

    In PHP:

    <?php
      if(stristr($_SERVER['HTTP_HOST'], "dir1.domain.com"))
      {
        header("Location: http://www.domain.com/dir1", TRUE, 301);
        exit();
      }
    ?>
    PHP:
    With RewriteRules:

    RewriteCond %{HTTP_HOST} dir1.domain.com [NC]
    RewriteRule .* http://www.domain.com/dir1 [R=301,L]
    Code (markup):
    Both versions will behave exactly the same way, each doing a case-insensitive comparison of the hostname with each directory name and if it matches the directory names, respond with a 301 redirect to the correct directory on the www domain.
     
    Ladadadada, Oct 14, 2007 IP
    sparksflying and Yfcadmin like this.
  3. sparksflying

    sparksflying Peon

    Messages:
    1,066
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for that , green added :)
     
    sparksflying, Oct 14, 2007 IP