htaccess重定向主机名到FQDN



我有一台本地机器,如果你输入http://host,它会转到上面的网站。我想使用htaccess重定向到http://host.domain.com。最后,它是相同的,我只是想使用适当的url用于文档目的。我试过这个,但没有工作

   Options +FollowSymlinks
   RewriteEngine on
   rewritecond %{http_host} ^host [nc]
   rewriterule ^(.*)$ http://host.domain.com/$1 [r=301,nc]`

使用NE标志来保持像?这样的字符的原始形式,而不是将它们转换成%-转义,从而失去它们的特殊意义。

然而,mod_rewrite是一个笨拙的方式做一个简单的重定向。如果您可以访问主/虚拟主机配置,那么最好使用Redirect,这是为这项工作设计的工具:

<VirtualHost *:*>
    ServerName host.domain.com
    ... real host settings...
</VirtualHost>
<VirtualHost *:*>
    ServerName host
    Redirect permanent / http://host.domain.com/
</VirtualHost>

您可以尝试在FQDN上进行负匹配-这样任何不是host.domain.com的内容都将被重定向为具有该名称。

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} "!^host.domain.com" [nc]
RewriteRule ^(.*)$ http://host.domain.com/$1 [r=301,nc]

最新更新