1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Problem excusing subdomains from .htaccess redirects

Discussion in 'Apache' started by PHP Bear, Feb 11, 2017.

  1. #1
    I'm having a weird problem with my subdomains. Two original subdomains work fine, but if I create a new subdomain - like /public_html/books - it redirects to www.books.mysite.com, instead of books.mysite.com.

    I've had to jump through a few hoops to make my URL's default to lower case, replace underscores with hyphens and default to https. So my .htaccess file is getting a little complex. Anyway, here's my webhost's analysis of the problem:

    I've looked into this and found that it is due to the rewrites you've got setup in /home/username/public_html/.htaccess, specifically the ones on lines 31 to 38, which first redirect to https (line 29) and then redirect to www if not already www (line31) and ultimately to https and www (line 34).

    This is affecting the subdomains because an .htaccess file affects sub-directories as well as the directory it exists within, so if you want these subdomains to not get redirected like this then you'll need to exclude them with a new rule in that file.

    So I checked out this page to learn how to NOT redirect a subdomain. This is the rule I'm now using, replacing "mysite" with my domain name, of course...

    RewriteCond %{HTTP_HOST} !^books.mysite.co$ [NC]

    But it isn't working. I'm not sure if I've written it incorrectly, or if I'm just not inserting it in the right place.

    Below is the code from my .htaccess file. Can someone tell me what I need to do to make this subdomain rule work? I should mention that I'm going to install a WordPress site on this subdomain, so I'm not concerned about lower-case defaults etc.

    * * * * *

    
        # Add /? before first $ in every row if you want optional trailing slashes.
        RewriteEngine On
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^test\.htm$ test.php [L]
        Options -MultiViews
    
        ##########
        # NEW RULES...
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^([^_\ ]*)[_\ ](.*?[_\ ].*) $1-$2 [N]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^([^_\ ]*)[_\ ]([^_\ ]*) /$1-$2 [L,R=301]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^(.*?[A-Z].*) /${tolower:$1} [R]
        ErrorDocument 404 /404.php
        ##########
    
        # Redirect traffic to https...
        RewriteCond %{HTTPS} off
        # First rewrite to HTTPS:
        # Don't put www. here. If it is already there it will be included, if not
        # the subsequent rule will catch it.
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
        # Now, rewrite any request to the wrong domain to use www.
        RewriteCond %{HTTP_HOST} !^www\.
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
        ##########
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^cp/([a-zA-Z0-9()_/-]+)/?$ cp/index.php?cp=$1 [L]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^introduction/([a-zA-Z0-9()_/-]+)/?$ introduction/index.php?intro=$1 [L]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^search/([a-zA-Z0-9()_/-]+)/?$ search/index.php?search=$1 [L]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^welcome/([a-zA-Z0-9()_/-]+)/?$ welcome/index.php?welcome=$1 [L]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^about/([a-zA-Z0-9()_/-]+)/?$ about/index.php?about=$1 [L]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^dictionary/([a-zA-Z0-9()_/-]+)/?$ dictionary/index.php?dict=$1 [L]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^calendar/([a-zA-Z0-9()_/-]+)/?$ calendar/index.php?cal=$1 [L]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^world/([a-zA-Z0-9()_/-]+)/?$ world/index.php?area=$1 [L]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^contact/([a-zA-Z0-9()_/-]+)/?$ contact/index.php?contact=$1 [L]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^club/([a-zA-Z0-9()_/-]+)/?$ club/index.php?club=$1 [L]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^zadmin/([a-zA-Z0-9()_/-]+)/?$ zadmin/index.php?zadmin=$1 [L]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^test/([a-zA-Z0-9()_/-]+)/?$ test/index.php?test=$1 [L]
    
        RewriteCond RewriteCond $1        !^(?:cp|introduction|search|welcome|about|dictionary|calendar|world|contact|club|admin|2016|test)(?:$|/)
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^/?([-a-zA-Z0-9_/]+)/?$ /index.php?encyc=$1 [L]
    
    Code (markup):
     
    PHP Bear, Feb 11, 2017 IP
  2. RoseHosting

    RoseHosting Well-Known Member

    Messages:
    230
    Likes Received:
    11
    Best Answers:
    11
    Trophy Points:
    138
    #2
    Adding the following rule:
    RewriteCond %{HTTP_HOST} !^books.mysite.com$
    Code (ApacheConf):
    under line 34 in your .htaccess file will do the trick.
     
    RoseHosting, Feb 23, 2017 IP
  3. PHP Bear

    PHP Bear Active Member

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #3
    Weird. When I insert that rule, nothing changes. But if I delete all the code after the new rule, the page displays this:

    Index of /
    • cgi-bin/
    • wordpress/
    ...which suggests it's working. But it's still pointing to the wrong URL...www.books.mysite.com.

    Between defaulting to lower case, https vs http and everything else, I may have used up my quota of tricks. ;)

    Another weird thing is that my two original subdomains work fine. But any new subdomain that I create doesn't work.

    Thanks for the tip, though. I'll hang on to that rule for future reference. I'll play around with my code, too, and see if I can make it work.
     
    PHP Bear, Feb 23, 2017 IP
  4. PHP Bear

    PHP Bear Active Member

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #4
    I deleted WordPress, then deleted all my .htaccess code except the rule you gave me. Then I deleted EVERYTHING, so I effectively had no .htaccess file at all. In both cases, the URL books.mysite.com defaults to www.books.mysite.com, and the page displays this:

    Index of /
    • cgi-bin/
    So maybe there's a deeper problem with .https or Apache.
     
    PHP Bear, Feb 23, 2017 IP