【问题标题】:How to redirect all/any page request on a site to a certain static page on same site如何将站点上的所有/任何页面请求重定向到同一站点上的某个静态页面
【发布时间】:2016-10-07 13:33:28
【问题描述】:

我需要站点上的每个页面请求(200、404 等)都到达站点上的固定页面,在本例中为 index_new.php

我已经尝试了其他 SO 和 ServerFault 答案,但我继续得到一个无休止的重定向循环。

这是我在 httpd.conf 中的代码:

<VirtualHost *:80>
    ServerName my.example.com
    ServerAlias *.my.example.com
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/index_new.php
    RewriteRule ^(.*)$ /index_new.php [R=301,L]
</VirtualHost>

注意事项:

  1. 网站的.htaccess 文件为空
  2. index_new.php”页面是静态的,没有 PHP 标头(“location:”)内部重定向等。
  3. 我已经重启了apache

【问题讨论】:

  • 规则看起来不错,您可以省略 RewriteCond 并将其缩短为 RewriteRule !^/index_new.php$ /index_new.php [R,L]。切勿使用 R=301 进行测试
  • 感谢@OlafDietsche。但是您的回答仍然会在我的 Chrome 浏览器中显示my.example.com redirected you too many times.(并不是说浏览器与它有任何关系——只是向您显示结果消息的格式)。
  • 我刚刚在本地尝试过,您的版本和我的版本都可以正常工作。也许还有别的东西。
  • 好的,谢谢@OlafDietsche。我去看看。
  • 我认为问题在于 &lt;link&gt; 容器中有一个 css 样式表 &lt;link&gt;'d。一旦我RewriteCond'd 了,我就可以走了。

标签: apache .htaccess redirect httpd.conf


【解决方案1】:

应该做一个负前瞻,并且可以通过 RedirectMatch 而不是使用 mod_rewrite 的单行轻松完成:

RedirectMatch permanent ^/(?!index_new.php).* /index_new.php

清除您的浏览器缓存和/或尝试使用curl -I http://my.example.com/wahtever 以确保它正常工作

为了健全,请在虚拟主机中指定 documentroot 和一个目录条目以允许访问(要求所有已授予/全部允许,具体取决于版本),并且由于您没有使用 .htaccess(顺便说一句,恭喜您!您是少数做事的人之一从一开始),为了安全起见,将 DocumentRoot 的目录设置为“AllowOverride none”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多