【问题标题】:Apache redirect from rootApache 从根目录重定向
【发布时间】:2015-10-21 15:21:31
【问题描述】:

我只是想知道如何在 Apache 中从 root 执行重定向。

我想检查是否有人转到根 url(例如example.com)并自动将他们重定向到example.com/h

我可以在 apache 配置或 .htaccess 文件中执行此操作吗?

【问题讨论】:

    标签: apache .htaccess redirect


    【解决方案1】:
    NameVirtualHost *:80
    
    <VirtualHost *:80>
            ServerName example.com
            RedirectMatch 301 ^/$ /h
    </VirtualHost>
    

    这将帮助您将所有请求从 example.com 重定向到 example.com/h

    【讨论】:

    • @w3d 请再举一些例子。我也是来这里学习的,不用担心投反对票:)
    • example.com/h 的目标 URL(即没有方案)将导致服务器立即出错(您将无法重新启动 Apache)。这需要完全合格,例如。 http://example.com/h 或根相关,例如。只需/h。但是,由于RedirectPermanent 是“前缀”匹配并且您正在重定向到同一服务器,这将导致重定向循环。 IE。 //h/hh/hhh 等等(因为它还在比赛之后附加了所有内容)。然而,这可以通过使用RedirectMatch 来轻松解决。例如:RedirectMatch 301 ^/$ /h 会修复它。 :)
    • 非常感谢 :) 以后会回答所有与 apache 相关的问题,如果我错了,你会帮助我,很高兴认识你 :)
    【解决方案2】:

    您可以在 Apache 配置或根 .htaccess 中使用此 mod_rewrite 规则:

    RewriteEngine On
    
    RewriteRule ^/?$ /h [L,R=302]
    

    验证此重定向规则后,将R=302 更改为R=301。如果您不想更改浏览器中的 URL,请使用:

    RewriteRule ^/?$ /h [L]
    

    【讨论】:

      【解决方案3】:

      您可以在 Apache config 或 .htaccess(每个目录配置文件)中执行此操作。

      在 .htaccess 中使用 mod_rewrite,从根目录重定向到特定 URL:

      Options +FollowSymLinks
      RewriteEngine On
      
      RewriteRule ^$ /h [R,L]
      

      请注意,这是一个临时 (302) 重定向。

      【讨论】:

        猜你喜欢
        • 2015-09-02
        • 2015-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-20
        • 2021-11-26
        • 2018-08-14
        相关资源
        最近更新 更多