Rewrite Question

Discussion in 'Apache' started by Ferrarislave, Apr 8, 2007.

  1. #1
    RewriteEngine On
    RewriteCond %{HTTP_HOST} .*.domain.com [NC]
    RewriteCond %{HTTP_HOST} !www.domain.com [NC]
    RewriteRule ^/(.*)$ /home/sitename/public_html/%{HTTP_HOST}/$1 [C]
    Code (markup):
    What exactly is the third line (the rewriterule) doing? Is it applying one of those rules that begin with %{HTTP_HOST}?
     
    Ferrarislave, Apr 8, 2007 IP
  2. sparckyz

    sparckyz Peon

    Messages:
    336
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The rewriterule is the line that actually does the work and rewrites your dynamic links into search engine friendly url.
     
    sparckyz, Apr 8, 2007 IP
  3. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The last line, RewriteRule is the only line that actually does something. The previous two lines, RewriteCond are conditions that must be met if the following rule is to apply. Basically, the RewriteConds ensure the hostname is a subdomain of domain.com, but not www.domain.com. If these conditions are met, then the rule kicks in and matches every request (the period meaning any character, * repeated one or more times) and rewrites to the appropriate directory.
     
    rodney88, Apr 8, 2007 IP
  4. Ferrarislave

    Ferrarislave Peon

    Messages:
    1,129
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This has nothing to do with search engine friendly urls.

    I'm aware of the rewrite conditions, I was asking why the %{http_host} is their after /public_html/. %{http_host} just returns the subdomain correct?
     
    Ferrarislave, Apr 8, 2007 IP
  5. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Actually you asked what the third line was doing, and referred to the previous two lines as "rules".

    But anyway %{VAR_NAME} contain server variables that can be used with mod_rewrite. HTTP_HOST is the hostname, which yes in this case will be sub.domain.com.
     
    rodney88, Apr 9, 2007 IP
  6. sparckyz

    sparckyz Peon

    Messages:
    336
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Returning the subdomain correctly would be more search engine friendly.
     
    sparckyz, Apr 9, 2007 IP
  7. Ferrarislave

    Ferrarislave Peon

    Messages:
    1,129
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #7
    So basically if the rewritecondition ${HTTP_HOST} is sub.testdomain.com but not www.testdomain.com it takes the "sub" part of the domain and rewrites it to the /home/sitename/pubic_html/sub/

    Is that correct? Would that directy need to be named /public_html/sub.testdomain.com or just sub ?
     
    Ferrarislave, Apr 10, 2007 IP
  8. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #8
    No. If you want to rewrite to a directory with just the subdomain part of the hostname, you'd need to capture it into a backreference first and use that. The HTTP_HOST variable contains the full hostname - including any subdomains if present.

    You can create a backreference by putting your regex pattern into parenthesis. Your current code will rewrite sub.domain.com to domain.com/sub.domain.com/

    This will rewrite sub.domain.com to domain.com/sub:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(www\.)?(.*).domain.com$ [NC]
    RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
    RewriteRule ^/(.*)$ /home/sitename/public_html/%2/$1 [C]
     
    rodney88, Apr 11, 2007 IP
  9. Ferrarislave

    Ferrarislave Peon

    Messages:
    1,129
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Ah I thought my current code would point subdomain.sitename.com to a folder on the system like /home/websitename/public_html/subdomain

    I was asking because I thought about having a sub domain for each user that points to a folder in the sites home directory.

    Also, why aren't you escaping the .'s in RewriteCond %{HTTP_HOST} ^(www\.)?(.*).domain.com$ [NC] and RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
     
    Ferrarislave, Apr 11, 2007 IP