简体   繁体   English

当主域指向另一个文件夹时,如何将子域重定向到文件夹?

[英]How can I redirect subdomain to folder while main domain points to another folder?

My very dear Stackoverflow community, 我非常亲爱的Stackoverflow社区,

I have the following redirection problem and after several unsuccessful attempts I come here in search of enlightenment. 我遇到以下重定向问题,经过几次失败的尝试后,我来到这里寻求启示。 My problem is the following. 我的问题如下。 I have a domain, let's call it 'www.mydomain.com', and my 'public_html' directory has two folders as follows: 我有一个域名,我们称之为“www.mydomain.com”和我的“的public_html”目录下有两个文件夹,如下所示:

public_html public_html

public_html/my_app/ public_html / my_app /

public_html/my_other_app/ public_html / my_other_app /

First, I would like that when typing the URL 'www.mydomain.com', I get redirected to the contents of folder 'my_app', while keeping the same URL. 首先,我想输入URL'www.mydomain.com'时,我将重定向到文件夹'my_app'的内容,同时保持相同的URL。 In fact this I have already accomplished, so whenever I type 'www.mydomain.com' I get redirected to 'www.mydomain.com/index.php', which actually corresponds to the 'public_html/myapp/index.php' script under 'myapp'. 实际上,我已经完成了此操作,因此无论何时键入“ www.mydomain.com”,我都将重定向到“ www.mydomain.com/index.php”,该地址实际上对应于“ public_html / myapp / index.php”脚本在“ myapp”下。

Now I want to have a subdomain called 'other.mydomain.com', which has to redirect to contents of the 'my_other_app' folder, but I do not know how to make .htaccess work for this and at the same time work for the first case also. 现在,我想拥有一个名为“ other.mydomain.com”的子域,该子域必须重定向到“ my_other_app”文件夹的内容,但是我不知道如何使.htaccess与此同时起作用,第一种情况也。

So this is basically, the main domain redirects to one folder, and a subdomain redirects to another folder, and both folders are located under the public_html directory 因此,基本上,这是主域重定向到一个文件夹,子域重定向到另一个文件夹,并且两个文件夹都位于public_html目录下

Any hints more than welcome. 任何提示都值得欢迎。

For your reference I post below my current .htaccess file: 供您参考,我在当前的.htaccess文件下方发布:

RewriteEngine On

# redirect to www prefix
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# if start with www and no https then redirect
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# rewrite URL to trim folder
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^$ /login [L,R=301]
RewriteRule ^(.*)$ test/$1 [L]

This actually works for my main domain, it also rewrites the url to https. 这实际上适用于我的主域,它也将URL重写为https。 I need to add something in here in order to process separately the 'other.mydomain.com' and redirect to the '/my_other_app/' subfolder 我需要在此处添加一些内容,以便分别处理“ other.mydomain.com”并重定向到“ / my_other_app /”子文件夹

what you need is a vhost (virtual host) per app. 你需要的是每一个应用程序虚拟主机(虚拟主机)。 In the vhost, you will define the vhosts root directory, which will point to either of your sub directories. 在虚拟主机中,您将定义虚拟主机根目录,该目录将指向您的任何子目录。

There is IP based vhosts (one IP address per subdomain) or name based vhosts (the vhost is chosen based on the HTTP host header that all modern browser send). 有基于IP的虚拟主机(每个子域一个IP地址)或基于名称的虚拟主机(虚拟主机是根据所有现代浏览器发送的HTTP主机标头选择的)。 But there is too much to say about vhosts to write it all here, just read the apache documentation here: 但是要在这里编写所有有关vhost的内容还有很多话要说,只需阅读apache文档即可:

http://httpd.apache.org/docs/2.2/vhosts/ http://httpd.apache.org/docs/2.2/vhosts/

I think with pure .htaccess files, you can't do that (I might be wrong). 我认为使用纯.htaccess文件无法做到这一点(我可能错了)。 Normally you would add vhosts in the main apache config. 通常你会在主Apache的配置添加虚拟主机。 Based on your hosting, this may not be possible. 根据您的托管,这可能无法实现。 Talk to you hosting provider in that case. 在这种情况下,请与您的托管服务提供商联系。

Marc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM