Forum Moderators: phranque

Message Too Old, No Replies

Apache / .htaccess help

Rewriting add-on domains to subdirectories

         

bendj

6:29 pm on Dec 1, 2004 (gmt 0)

10+ Year Member



Below is what I am using right now, to host 3 sites on the same server. One is the main site - operated out of the main public html directory. The other two are out of directories.

Two questions:
When someone types in www.domain2.com/something (with a subdirectory), then they are redirected to www.maindomain.com/something. I am wanting it to stay at domain2.com.

Secondly, with the code below, am I doing everything correctly, are there any ways to fix it up or change it to make it better? I want to avoid trailing slash problems and be able to easily host multiple domains on my server.

Thanks for the help!

Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on

#Send domain2.com to subdirectory /domain2/
RewriteCond %{HTTP_HOST} ^(ww+\.)?domain2\.com
RewriteCond %{REQUEST_URI}!/domain2/
RewriteRule ^(.*)$ /domain2/$1 [L]

#Send domain3.com to subdirectory /domain3/
RewriteCond %{HTTP_HOST} ^(ww+\.)?domain3\.com
RewriteCond %{REQUEST_URI}!/domain3/
RewriteRule ^(.*)$ /domain3/$1 [L]

RewriteCond %{HTTP_HOST}!^www\.maindomain\.com [NC]
RewriteCond %{HTTP_HOST}!^www\.domain3\.com [NC]
RewriteCond %{HTTP_HOST}!^domain3\.com [NC]
RewriteCond %{HTTP_HOST}!^www\.domain2\.com [NC]
RewriteCond %{HTTP_HOST}!^domain2\.com [NC]
RewriteCond %{SERVER_PORT}!^443$
RewriteRule (.*) [maindomain.com...] [R=301,L]

</IfModule>

jdMorgan

7:54 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



bendj,

When someone types in www.domain2.com/something (with a subdirectory), then they are redirected to www.maindomain.com/something. I am wanting it to stay at domain2.com.

I believe this redirection is taking place because any request which is not rewritten by the first two rulesets will fall through to the third ruleset, where any requests not made to port 443 will be redirected to your main domain. It's not clear what you intended to do with that test for port 443, so I can't offer any advice.

Secondly, with the code below, am I doing everything correctly, are there any ways to fix it up or change it to make it better? I want to avoid trailing slash problems and be able to easily host multiple domains on my server.

You might want to take a look at the thread, Rewrite arbitrary subdomains to subdirectories [webmasterworld.com] (See msg #6). The method described there will allow you to create any number of subdomains without adding any more code to your .htaccess file.

Jim

bendj

4:32 am on Dec 2, 2004 (gmt 0)

10+ Year Member



Thanks for the help..
A couple things.

I do not have access to my host file so I have to do it through htaccess :(

Basically I put!^443 because I did not want the rewrite rules to apply to https.

bendj

4:33 am on Dec 2, 2004 (gmt 0)

10+ Year Member



Is there a way to have the rewrite rules for the subdirectories catch what I meantioned before?

Basically I am wanting total masking effect for those other domains.

jdMorgan

8:28 pm on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well...

Combining the code technique in the thread I cited above with your main domain redirect, we'd get:


# IF hostname header is non-blank
RewriteCond %{HTTP_HOST} .
# And IF not main domain
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com [NC]
# (get domain name from requested host header)
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.com
# And IF a subdirectory by that name does not exist
RewriteCond /%2 !-d
# AND IF not port 443
RewriteCond %{SERVER_PORT} !^443$
# Then externally redirect to main domain
RewriteRule (.*) http://www.maindomain.com/$1 [R=301,L]
#
# Internally rewrite <domain>.com/<path> to <domain>.com/<domain>/<path> except for main domain
# IF hostname header is non-blank
RewriteCond %{HTTP_HOST} .
# And IF not main domain
RewriteCond %{HTTP_HOST} !^(www\.)?maindomain\.com [NC]
# Extract (required) domain (%2), and first path element (%4), discard port number if present (%3)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^(www\.)?([^.]+)\.com(:[0-9]{1,5})?<>/([^/]+) [NC]
# Rewrite only when domain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %2<>%4 !^([^<]+)<>\1$ [NC]
# Rewrite /path to /domain/path
RewriteRule ^(.*) /%1/$1 [L]

Two ways to describe the first rule:
  • The rule will be invoked if the HTTP_HOST header is non-blank, a secondary domain is requested and no corresponding subdirectory exists, and the request is not made to port 443.
  • The rule will not be invoked if the HTTP_HOST header is blank (HTTP/1.0 clients do not provide this header), if the main domain is requested, if a subdirectory exists for the requested domain, or if port 443 is requested.

    Because my time is limited, I haven't tested this code, but it's designed to do what you want, and to allow you to add as many secondary domains as you like without changing the code. As long as you create a subdirectory to host the additional domains, those domains will be accepted. Otherwise, the request will be redirected to your main domain.

    I have used the words "rewrite" and "redirect" carefully. A rewrite is internal to the server and will not update the browser address bar. A redirect is external, and will update the browser address bar, because the browser is involved in the transaction. If an internal rewrite appears to update the address bar, it is because of an error in the code or because a subsequent redirect is also invoked.

    I think you can probably tell why the reply took awhile... ;)

    Jim

  • bendj

    8:59 pm on Dec 3, 2004 (gmt 0)

    10+ Year Member



    Wow thanks for the great response Jim.
    Well im not getting any actual errors (internal server error or anything) but when I type something like www.domain2.com it goes to the www.maindomain.com page.

    bendj

    9:02 pm on Dec 3, 2004 (gmt 0)

    10+ Year Member



    Jim,

    I made have made a mistake. I see that now my subdirectories have to be named the same as the domain.

    I tried making this change. Do the subdirectories have to be named *.com or whatever or just the main address? Like for www.domain2.com just domain2 correct? If so yeah it is just sending these requests to maindomain.

    Thanks

    jdMorgan

    10:32 pm on Dec 3, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    > Like for www.domain2.com just domain2 correct?

    Yes, but you can change it if you like. Just change the code where the domain names are extracted.

    Jim

    jdMorgan

    2:57 am on Dec 4, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    I spotted a bug in the code. Change the bolded line below.

    # IF hostname header is non-blank
    RewriteCond %{HTTP_HOST} .
    # And IF not main domain
    RewriteCond %{HTTP_HOST} !^www\.maindomain\.com [NC]
    # (get domain name from requested host header)
    RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.com
    # And IF a subdirectory by that name does not exist
    RewriteCond [b]%{DOCUMENT_ROOT}/[/b]%2 !-d
    # AND IF not port 443
    RewriteCond %{SERVER_PORT} !^443$
    # Then externally redirect to main domain
    RewriteRule (.*) http://www.maindomain.com/$1 [R=301,L]
    #
    # Internally rewrite <domain>.com/<path> to <domain>.com/<domain>/<path> except for main domain
    # IF hostname header is non-blank
    RewriteCond %{HTTP_HOST} .
    # And IF not main domain
    RewriteCond %{HTTP_HOST} !^(www\.)?maindomain\.com [NC]
    # Extract (required) domain (%2), and first path element (%4), discard port number if present (%3)
    RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^(www\.)?([^.]+)\.com(:[0-9]{1,5})?<>/([^/]+) [NC]
    # Rewrite only when domain not equal to first path element (prevents mod_rewrite recursion)
    RewriteCond %2<>%4 !^([^<]+)<>\1$ [NC]
    # Rewrite /path to /domain/path
    RewriteRule ^(.*) /%1/$1 [L]

    Jim

    bendj

    12:03 am on Dec 5, 2004 (gmt 0)

    10+ Year Member



    Jim,

    Ok I tried the code where you fixed the bug. It does not loop back to the main domain now. However, it is causing a internal server error. :(

    jdMorgan

    6:01 pm on Dec 5, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Without seeing what's in your error log, there's not much I can suggest, except the following:

    I found out last night that not all servers will support the "comparision" operation used in that code. The problem is that the "\1" local back-reference is a posix 1003.2 regular expressions construct, and not all operating systems have upgraded to posix 1003.2. I know it works under FreeBSD, and does not work under Unix.

    So that means you'll have to hard-code the names of your add-on domains in the code. :(

    Try hard coding just one subdomain name, and if that fixes the Server Error, then you'll know.

    Jim

    bendj

    4:03 pm on Dec 9, 2004 (gmt 0)

    10+ Year Member



    Ok so if that is the case (it doesnt work), then should i go back to something more like what I posted in the first place?

    If so then I am basically back to my same question in the original post.

    It is not a problem for me to hardcode the subdirectories in as there will only be about 3-4.

    jdMorgan

    5:02 pm on Dec 9, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Yes, you can change the second block of code to something like:

    RewriteCond $1 !^(subdomain1存ubdomain2存ubdomain3存ubdomain4)/
    RewriteCond %{HTTP_HOST} ^(subdomain1存ubdomain2存ubdomain3存ubdomain4)\.yourdomain\.com
    RewriteRule ^(.*) /%1/$1 [L]

    Jim

    bendj

    7:37 pm on Dec 13, 2004 (gmt 0)

    10+ Year Member



    I am not sure what you mean?
    I am not using the format subdomain.domain.com.

    I simply have folders inside my root directory which are used as content for the other domains which have totally seperate domain names.

    www.maindomain.com
    www.seconddomainname.com (reads from certain folder)
    etc.

    See what I mean?
    Thanks.

    bendj

    7:54 pm on Dec 13, 2004 (gmt 0)

    10+ Year Member



    The last code you gave did not work. I do not understand how that is suppose to work because it does not detect my other domains... www.domain2.com etc.

    Thanks

    jdMorgan

    8:19 pm on Dec 13, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Sorry, maybe this is closer. But I can't write and test your code for you, so please see the references in our charter [webmasterworld.com] and make sure you understand what this code does. If it's not right, then you can modify it to suit you needs.

    # Make sure we haven't already rewritten to an alternate domain's subdirectory
    RewriteCond $1!^(domain1圬omain2圬omain3圬omain4)/
    # Otherwise, extract requested domain name from HTTP_HOST request header
    RewriteCond %{HTTP_HOST} ^(www\.)?(domain1圬omain2圬omain3圬omain4)\.com
    # Rewrite to requested page in appropriate subdirectory
    RewriteRule ^(.*) /%1/$1 [L]

    Jim