how to add a sub domain .htaccess pointing to a folder outside the root

Deadly 提交于 2019-12-11 07:57:26

问题


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?


回答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.



来源:https://stackoverflow.com/questions/13202622/how-to-add-a-sub-domain-htaccess-pointing-to-a-folder-outside-the-root

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!