代码之家  ›  专栏  ›  技术社区  ›  Tom

使用.htaccess重定向

  •  2
  • Tom  · 技术社区  · 16 年前

    我要将所有用户页请求重定向到同一域中的某个页。

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

    我试着用这个:

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

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

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

    除了那个页面,你知道怎么写这个规则吗?

    4 回复  |  直到 13 年前
        1
  •  2
  •   Gumbo    16 年前

    必须排除要重定向到的文件。这里有一个使用mod_rewrite的例子:

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule !^index2\.php$ /index2.php [L,R=302]
    </IfModule>
    
        2
  •  1
  •   mtk    13 年前

    要防止无休止的循环(index2.php<-->index2.php):

    仅当要重定向的URL不包含字符串“index2.php”时重定向

    RewriteCond %{QUERY_STRING} !index2.php
    RewriteRule ^(.*)$ /index2.php [L,R=301]
    
        3
  •  0
  •   Eddy    16 年前

    你可以用mod重写

    <IfModule mod_rewrite.c>
    RewriteCond {REQUEST_URI} !=/index2.php
    RewriteEngine on
    RewriteRule ^.*$ /index2.php
    # End .HTACCESS
    </IfModule>

        4
  •  0
  •   Rob Wells    16 年前

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

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

    这将使您返回重定向的临时移动状态。

    省略这组标志意味着mod重写将在默认情况下返回302找到的状态。

    高温高压

    干杯,

    推荐文章