我对这件事有意见。我已经尝试了带有rewriterule和rewritecond的.htaccess方法,以及virtualdirectory方法。以下是我为virtualhost所做的尝试:
<VirtualHost *:80>
ServerName blank.example.com
Redirect permanent / "http://example.com/blank/blank"
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
ServerAdmin webmaster@localhost
DocumentRoot /the/doc/root
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
这是给.htaccess的:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blank.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/blank/blank [R=301,L]
我不确定是否需要,但是blank.example.com也指向/etc/hosts中的localhost
关于我遗漏了什么有什么建议吗?
最佳答案
这里有几件事。
如果您只想让subdomain
具有自己的文档根目录,那么vhost需要如下所示。那么就不需要重定向了。
<VirtualHost *:80>
ServerName blank.example.com
DocumentRoot /path/to/blank/blank
<Directory /path/to/blank/blank>
#enable .htaccess for this subdomain
AllowOverride All
</Directory>
</VirtualHost>
如果你真的想把你的子域重定向到主域。
然后移除第一个vhost。根本不需要。
#This virtualhost is not needed
<VirtualHost *:80>
ServerName blank.example.com
Redirect permanent / "http://example.com/blank/blank"
</VirtualHost>
然后在主虚拟主机上添加一个服务器别名,如下所示
ServerName example.com
ServerAlias blank.example.com
ServerAdmin webmaster@localhost
DocumentRoot /the/doc/root
<Directory /the/doc/root>
#enable .htaccess for this main domain
AllowOverride All
</Directory>
那么对于您的.htaccess规则
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blank\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/blank/blank/$1 [R=301,L]
在所有配置文件更改后重新启动apache。
关于linux - Apache2,将blank.example.com重定向到example.com/blank/blank,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33618895/