1

I want to have a sub domain that points to a folder outside the normal public root folder.

httpdocs/
sub_dom/

So say "admin.domain.com" points to "sub_dom" while "domain.com" points to the regular httpdocs folder.

  1. is this possible
  2. is there a better solution (for example would it be better/more advisable to do it another way)

Currently the htaccess file contains this but it does not seem to do the trick:

RewriteCond %{HTTP_HOST} ^admin\.domain\.com$
RewriteRule ^/(.*) /var/www/vhosts/domain.com/sub_dom/admin/ [redirect,last]

There is most probably something very wrong with the second line maybe?

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
KB.
  • 3,549
  • 4
  • 23
  • 29

1 Answers1

1

is this possible

It is in vhost or server config. It is not possible using an htaccess file unless you can already access the /sub_dom/ directory via your regular domain. You can't access any directory outside of your document root using your htaccess file.

is there a better solution (for example would it be better/more advisable to do it another way)

The correct way to do it is to create a new vhost. Your old vhost should have server names something like this:

ServerName domain.com
ServerAlias www.domain.com

In your new vhost, you have:

ServerName admin.domain.com

And a:

DocumentRoot /path/to/sub_dom/

The Apache documentation has a tutorial for setting up vhosts.

Alternatively, you can create an alias from your regular domain, something like:

Alias /sub_dom /path/to/sub_dom

Then you can use mod_rewrite in your domain.com document root (httpdocs):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^admin\.domain\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/sub_dom%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/sub_dom%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/sub_dom%{REQUEST_URI} -s
RewriteRule ^ /sub_dom%{REQUEST_URI} [L]

But, you may as well just move /sub_dom/ into httpdocs, it'll achieve the same thing.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • This exactly what I am looking. I know that `vhosts` is the solution to point subdomain to outside root. I just realized that it can be done also with `alias`. – eQ19 Jan 07 '16 at 10:30
  • I am on a shared hosting plan and am not sure where to set up either of these alternatives in cpanel - or even if I can. Secondly, you wrote that moving /sub_dom/ into httpdocs will achieve the same thing - but won't that make those folders/files accessible from two URLs then? admin.domain.com and domain.com/admin ? – youcantryreachingme Aug 22 '18 at 12:19