AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 120488
Accepted
DisgruntledGoat
DisgruntledGoat
Asked: 2010-03-09 12:56:25 +0800 CST2010-03-09 12:56:25 +0800 CST 2010-03-09 12:56:25 +0800 CST

Apache VirtualHost 中的重定向 URL?

  • 772

我有一个带有 Apache 的专用服务器,我在上面设置了一些 VirtualHosts。我已经设置了一个来处理 www 域以及非 www 域。

我的 www 的 VH .conf 文件:

<VirtualHost *>
  DocumentRoot /var/www/site
  ServerName www.example.com
  <Directory "/var/www/site">
    allow from all
  </Directory>
</VirtualHost>

有了这个.htaccess:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

有没有一种简单的方法可以将 www 重定向到非 www 版本?目前我将两个版本发送到相同的版本DocumentRoot并使用.htaccess,但我确信我必须能够在 VirtualHost 文件中做到这一点。

apache-2.2 virtualhost redirect mod-rewrite
  • 4 4 个回答
  • 270895 Views

4 个回答

  • Voted
  1. Best Answer
    DisgruntledGoat
    2010-03-09T13:34:37+08:002010-03-09T13:34:37+08:00

    事实证明mod_rewrite,除了规则之外,VirtualHosts 文件中的规则很好RewriteBase。我最终得到了这个:

    <VirtualHost *>
      ServerName www.example.com
      RewriteEngine on
      RewriteCond %{HTTP_HOST} ^www.example.com
      RewriteRule ^/(.*)$ http://example.com/$1 [L,R=301]
    </VirtualHost>
    

    编辑:根据 joschi 在评论中的建议,我现在使用以下Redirect指令使用这个简化版本mod_alias:

    <VirtualHost *>
      ServerName www.example.com
      Redirect 301 / http://example.com/
    </VirtualHost>
    
    • 122
  2. Marco Trevisan
    2013-01-19T03:29:18+08:002013-01-19T03:29:18+08:00

    对 301 重定向要非常小心,因为默认情况下,接收 301 重定向的浏览器会将其永久存储 - 这意味着您将放弃控制该浏览器在尝试访问域时将看到的内容www.example.com。

    例如,请参阅此讨论http://getluky.net/2010/12/14/301-redirects-cannot-be-undon/

    所以要么确保它没有被缓存,要么使用 mod_proxy(我推荐 mod_proxy)。

    如果您可以让用户看到浏览器地址栏上的 URL 更改,请使用 mod_rewrite:

    <VirtualHost *>
     ServerName www.example.com
     RewriteEngine on
     RewriteCond %{HTTP_HOST} ^www.example.com
     RewriteRule ^/(.*)$ http://example.com/$1 [L,R=301,E=nocache:1]
    ## Set the response header if the "nocache" environment variable is set
    ## in the RewriteRule above.
     Header always set Cache-Control "no-store, no-cache, must-revalidate" env=nocache
    ## Set Expires too ...
     Header always set Expires "Thu, 01 Jan 1970 00:00:00 GMT" env=nocache
    </VirtualHost>
    

    如果您希望“重定向”对用户不可见,请使用 mod_proxy:

    <VirtualHost *>
     ServerName www.example.com
     ProxyRequests Off
     <Proxy *>
     Order Deny,Allow
     Deny from all
     Allow from 203.0.113.67
     </Proxy>
     ProxyPass / http://example.com/
     ProxyPassReverse / http://example.com/
    </VirtualHost>
    

    应该注意的是,如果 mod_proxy 配置不当,可能会损害您的网络。

    • 4
  3. Warner
    2010-03-09T13:00:23+08:002010-03-09T13:00:23+08:00

    您可以添加ServerAlias example.com到,VirtualHost但性能将不同于重定向。

    编辑

    由于您想要重定向并且您不需要高级功能,因此似乎 usingRedirect对您来说就足够了。你可以把它Redirect放在一个 VirtualHost 指令下。

    客户端解决方案是使用meta refresh标签。

    • mod_alias 文档
    • 元刷新
    • 2
  4. Devin Ceartas
    2010-03-09T13:18:40+08:002010-03-09T13:18:40+08:00

    好吧,您可以为 SERVERNAME www.example.com 创建一个虚拟主机,并将其重定向到另一个具有服务器名称 example.com 的虚拟主机

    • 1

相关问题

  • 如何强制我的网址始终以 www 开头?

  • 在 Linux Xen VPS 上优化 Apache 和 MySQL

  • mod_rewrite 不转发 GET 参数

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve