假设我有一个 IP 地址为 1.2.3.4 的 Web 服务器。
我还有 2 个子域名:
sth.city.eu
inf.xyz.city.eu
我在 Ubuntu Server 上使用 apache 2。这是我的/etc/apache2/sites-enabled/site
文件:
<VirtualHost *:80>
ServerName www.sth.city.eu
ServerAlias sth.city.eu
ServerAlias inf.xyz.city.eu
ServerAlias www.inf.xyz.city.eu
ServerAdmin webmaster@localhost
DocumentRoot /var/www/city/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/city/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/error_city.log
CustomLog ${APACHE_LOG_DIR}/access_city.log combined
</VirtualHost>
我想做这样的事情:如果有人访问inf.xyz.city.eu
,服务器应该自动将他重定向到sth.city.eu
(更改 URL)。这可能吗 - 我该怎么做?我在 Google 上搜索并找到了Redirect
,尝试像这样使用它:
Redirect http://inf.xyz.city.eu http://www.sth.city.eu
但它没有起作用。
答案1
我相信你可以通过设置 2 个虚拟主机来解决这个问题:
<VirtualHost *:80>
ServerName inf.xyz.city.eu
ServerAlias www.inf.xyz.city.eu
Redirect / http://sth.city.eu/
</VirtualHost>
<VirtualHost *:80>
ServerName www.sth.city.eu
ServerAlias sth.city.eu
ServerAdmin webmaster@localhost
[...]
</VirtualHost>
答案2
如果你有 mod_rewrite,你可以使用 RewriteRules:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^inf\.xyz\.city\.eu [NC]
RewriteRule (.*) http://sth.city.eu/$1 [R=301,QSA,L]
答案3
@mikolan 所说的是:
你应该/etc/apache2/sites-enabled/
有2个vhost
文件:
- 文件:
/etc/apache2/sites-enabled/sth.city.eu
<VirtualHost *:80>
ServerName sth.city.eu
ServerAlias www.sth.city.eu
ServerAdmin webmaster@localhost
DocumentRoot /var/www/city/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/city/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/sth.city.eu.error.log
CustomLog ${APACHE_LOG_DIR}/sth.city.eu.access.log combined
</VirtualHost>
- 文件:
/etc/apache2/sites-enabled/inf.xyz.city.eu
<VirtualHost *:80>
ServerName inf.xyz.city.eu
ServerAlias www.inf.xyz.city.eu
Redirect / http://sth.city.eu/
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/inf.xyz.city.eu.error.log
CustomLog ${APACHE_LOG_DIR}/inf.xyz.city.eu.access.log combined
</VirtualHost>
当然,这两个文件都是在中创建的/etc/apache2/sites-available
,并且启用了
sudo a2ensite inf.xyz.city.eu
sudo a2ensite sth.city.eu
sudo service apache2 restart
每次启用/禁用站点/模块 a
reload
就足够了,但在测试时restart
更安全(心境平和)检查mod_alias已启用(应为默认启用)
- 检查日志以查看您遇到了什么错误:权限不足,甚至没有收到请求?