Forum Moderators: phranque

Message Too Old, No Replies

Is this possible for RewriteRule

.htaccess for sub-domains

         

psychophat

6:39 am on Jul 22, 2007 (gmt 0)

10+ Year Member



How can you do this using RewriteRule on .htaccess

domain.com/sites/sub1 -> sub1.domain.com
domain.com/sites/sub2 -> sub2.domain.com
domain.com/sites/sub3 -> sub3.domain.com

My .htaccess used to make this flow work but for some reason when the host upgraded everything went haywire. Old script below:

RewriteEngine on
RewriteCond %{HTTP_REFERER}!^http://domain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://domain.com$ [NC]
RewriteCond %{HTTP_REFERER}!^http://sub2.domain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://sub2.domain.com$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.domain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.domain.com$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.sub2.domain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.sub2.domain.com$ [NC]

Don't know if somehow I have overwritten the old .htaccess but this is the format that is left on the server.

Can someone enlighten me on the correct way to structure this.

psychophat

9:27 am on Jul 22, 2007 (gmt 0)

10+ Year Member



Is sym links the answer but that would make the same amount of sym links of subdomains in the root directory. Any other option?

I'm browsing each thread and still can't find a solution for this.

psychophat

11:11 am on Jul 22, 2007 (gmt 0)

10+ Year Member



I was reading thread after thread and as I understand it to make it possible I have to make it like this:

move root to root/sites/

and place .htaccess under sites so that all sub-domains will have something like:

domain.com/sites/subdomain1 -> subdomain1.domain.com

but if it's done like that won't it mess up the main site on the root domain.com/index.*

I'm hessitant to try it and mess things up again. Gambling on the chips and bits from the threads wish me luck*

I'm following this as my bible: [webmasterworld.com...]

jdMorgan

4:41 pm on Jul 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code you've posted is apparently part of (the RewriteRule is missing) some auto-generated (poorly-coded) cPanel anti-hotlinking code.

The normal method for mapping subdomains to subdirectories is to:

  • Internally rewrite sub1.example.com/* to /sites/sub1/*
    and
  • externally redirect direct client requests for /sites/sub1/* back to sub1.example.com/*

    This is tricky, because it might easily result in an 'infinite' redirect-rewrite loop if you don't take steps to prevent it.

    You must use {THE_REQUEST} to distinguish between direct client requests for /sites/sub1/* from the internal requests for that subdirectory that result from the internal rewrite.

    I'll show you some example code, but only if you promise to make and maintain backups of your critical files from now on... :)


    # Externally redirect direct client requests for example.com/sites/<subdomain> back to <subdomain>.example.com
    # (Prevents duplicate-content problems and discourages users from poking around in the file system)
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /sites/
    RewriteRule ^sites/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]
    #
    # Internally rewrite <subdomain>.example.com requests to /sites/<subdomain> subdirectory
    # Don't rewrite to /sites if we've already rewritten (prevents a rewriting loop)
    RewriteCond $1 !^sites/
    # Exclude main domain and its www subdomain from this rewrite
    RewriteCond %{HTTP_HOST} !^(www\.)?example\.com
    # Get subdomain to %1 variable
    RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
    # Internally rewrite <subdomain> to /sites/<subdomain> subdirectory
    RewriteRule (.*) /sites/%1/$1 [L]

    This method is simple, but it has one limitation: You cannot use a subdomain named "sites". There are other methods to get around this, but they don't work on all Apache configurations and so are best left alone unless absolutely required.

    As for your anti-hotlinking code, I don't know what the RewriteRule was, so I can't help you there. But I will caution you NOT to use cPanel functions which add code to .htaccess -- cPanel simply writes awful, inefficient code, and can slow down your server. Either study-up on the Apache directives available in .htaccess and code it yourself, or consider doing without those functions...

    Jim

  •