【问题标题】:how to set conditional DirectoryIndex using .htaccess如何使用 .htaccess 设置条件 DirectoryIndex
【发布时间】:2014-07-14 13:19:47
【问题描述】:

我想尝试布局,看看哪些有效,哪些无效。我不想对所有流量都这样做,以防万一出现问题。为此,我想将部分流量重定向到单独的文件。我想使用 .htaccess 来做到这一点。我的第一次尝试是重定向 IP 地址以 1 或 2 结尾的人以获得新布局。我想动态设置 DirectoryIndex 来做到这一点。

这就是我所拥有的:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

#split 20% of the traffic to go to the alternative layout
RewriteCond %{REMOTE_ADDR} "*(1|2)$"
RewriteRule .? - [S=4]
RewriteRule ^fb/([^/]+) /fb.php?id=$1 [nc]
RewriteRule ^classic /?layout=classic [QSA,nc]
RewriteRule ^thumbs /?layout=thumbs [QSA,nc]
DirectoryIndex 3.php

#the rest of the traffic goes to the default layout
RewriteCond %{REMOTE_ADDR} "!(1|2)$"
RewriteRule .? - [S=1]
DirectoryIndex index.php index.html

AddDefaultCharset utf-8

但这并没有达到预期的效果,如果我能在 .htaccess 中有这个伪代码,我会很高兴:

if %{REMOTE_ADDR} matches "*(1|2)$"
   DirectoryIndex 3.php
else
   DirectoryIndex index.php

【问题讨论】:

    标签: apache .htaccess mod-rewrite redirect


    【解决方案1】:

    直接翻译这个块

    if %{REMOTE_ADDR} matches "*(1|2)$"
       DirectoryIndex 3.php
    else
       DirectoryIndex index.php
    

    我们可以像这样使用mod_rewrite 规则:

    # default value of DirectoryIndex
    DirectoryIndex index.php
    
    RewriteEngine On
    
    # use 3.php instead if following condition is true
    RewriteCond %{REMOTE_ADDR} (1|2)$
    RewriteRule ^(.+?/)?index\.php$ $13.php [L,NC]
    

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      • 2018-02-07
      • 2023-03-22
      相关资源
      最近更新 更多