【问题标题】:Apache2, re-direct blank.example.com to example.com/blank/blankApache2,将 blank.example.com 重定向到 example.com/blank/blank
【发布时间】:2016-02-10 16:12:52
【问题描述】:

我在使用此重定向时遇到了问题。我已经尝试使用 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

关于我缺少什么的任何提示?

【问题讨论】:

  • 您是否尝试更改 URL 以使用带有子文件夹的主域而不显示子域?还是要使用浏览器中的子域和子文件夹中的服务器文件?
  • 基本上我想把它变成一个 URL 缩短器。所以我真的不在乎它是否显示了它重定向到的完整 URL,但用户需要能够点击较短的链接并到达该重定向的 URL

标签: linux apache .htaccess mod-rewrite


【解决方案1】:

这里有几件事。

如果您只想拥有一个带有自己的文档根目录的subdomain,那么您的虚拟主机需要看起来像这样。那么就不需要重定向了。

<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> 

如果您真的想将子域重定向到主域。

然后删除第一个虚拟主机。它根本不需要。

 #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。

【讨论】:

  • 这让我朝着正确的方向前进,但它只是将我带到了 example.com 的根目录,而不是 example.com/blank/blank
  • 你对我的回答做了哪一部分?
  • 第二部分,所以我添加了一个别名并尝试了RewriteCond
猜你喜欢
  • 2018-11-22
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-13
相关资源
最近更新 更多