【问题标题】:Redirect using .htaccess使用 .htaccess 重定向
【发布时间】:2010-10-19 03:34:12
【问题描述】:

我想将所有用户页面请求重定向到同一域中的页面。

例如,我有一个“正在建设中,BRB”页面,我希望所有用户在尝试访问网站上的任何页面时都能看到该页面。

我试过用这个:

Redirect 302 / http://www.domain.com/index2.php

这样做的目的是尝试将重定向也应用到 index2.php 页面,但它会卡在一个循环中,然后用户会看到这个循环,直到浏览器停止。

http://www.domain.com/index2.phpindex2.phpindex2.phpindex2.php etc., etc,

关于如何将该规则写入除该页面之外的任何想法?

【问题讨论】:

    标签: apache .htaccess http-status-code-302


    【解决方案1】:

    防止无限循环(index2.php index2.php):

    仅重定向如果要重定向的 URL 不包含字符串 'index2.php'

    RewriteCond %{QUERY_STRING} !index2.php
    RewriteRule ^(.*)$ /index2.php [L,R=301]
    

    【讨论】:

      【解决方案2】:

      您必须排除要重定向到的文件。下面是一个 mod_rewrite 的例子:

      <IfModule mod_rewrite.c>
          RewriteEngine on
          RewriteRule !^index2\.php$ /index2.php [L,R=302]
      </IfModule>
      

      【讨论】:

      • 我可能做错了,但是当我尝试这个时,我的浏览器返回“重定向太多”错误。
      • 我将如何编写这样它就不会重写正确显示页面所需的 CSS 文件?
      • 不需要指定302返回,mod_rewrite默认会这样做。
      • @Tom:添加 RewriteCond 指令以排除其他 URI 路径:RewriteCond %{REQUEST_URI} ^/(css|images|foobar)/`.
      • 我想我们已经到了,但是当我添加它时,重定向停止工作.... RewriteEngine on RewriteCond %{REQUEST_URI} ^/(_assets)/ RewriteRule ! ^index2\.php$ /index2.php [L,R=302]
      【解决方案3】:

      我更倾向于使用稍微不同的

      Options +FollowSymLinks 
      <IfModule mod_rewrite.c>
          RewriteEngine on 
          RewriteRule ^/.*$ /index2.php [R=307,L]
      </IfModule>
      

      这将让您为重定向返回一个已移动的临时状态。

      省略这组标志意味着 mod_rewrite 默认会返回 302 Found 状态。

      HTH

      干杯,

      【讨论】:

      • 几张图片,包括页眉和页脚信息,这可能是导致它的原因吗?意味着它正在尝试重定向包含文件?
      • 哦,仅供参考,我已经尝试了上面的代码,现在它根本不做任何重定向......
      【解决方案4】:

      你可以使用 mod_rewrite

      &lt;IfModule mod_rewrite.c&gt;
      RewriteCond {REQUEST_URI} !=/index2.php
      RewriteEngine on
      RewriteRule ^.*$ /index2.php
      # End .HTACCESS
      &lt;/IfModule&gt;

      【讨论】:

      • 抱歉,忘记排除您要提供的文件。我已经更新了。
      • 感谢 Eddy,但我仍然收到内部服务器错误,有什么想法吗?
      • Hrm,你能添加一些日志,例如)RewriteLogLevel 9 /path/to/logs
      猜你喜欢
      • 2011-03-01
      • 2015-10-18
      • 2013-12-05
      • 2018-03-12
      • 2018-05-10
      相关资源
      最近更新 更多