【问题标题】:conditional DirectoryIndex in .htaccess.htaccess 中的条件 DirectoryIndex
【发布时间】:2023-03-03 10:47:01
【问题描述】:

是否可以根据 IP 使 .htaccess 文件中的 DirectoryIndex 值有条件,这样 - 例如 - 我的 IP 将 DirectoryIndex 视为 index.html 而其他人将 DirectoryIndex 视为 index.php?

除了mod_rewrite,还有其他解决方案吗?

【问题讨论】:

    标签: apache .htaccess conditional


    【解决方案1】:

    使用提供的信息,我相信您需要以下内容:

    RewriteCond %{REMOTE_ADDR} ^your_ip$
    RewriteRule (.*)/$ $1/index.php
    
    RewriteCond %{REMOTE_ADDR} !^your_ip$
    RewriteRule index.php$ index.html
    

    这样只有你的 IP 可以看到 index.php,其他人都会看到 index.html

    或者可能:

    DirectoryIndex index.html
    
    RewriteCond %{REMOTE_ADDR} ^your\.ip\.000\.000$
    RewriteRule ^index.html$ index.php
    

    【讨论】:

    • 这对我有用,但是如何添加多个 ip? DirectoryIndex index.html RewriteCond %{REMOTE_ADDR} ^your\.ip\.000\.000$ RewriteRule ^index.html$ index.php 谢谢
    【解决方案2】:

    据我所知,DirectoryIndex 没有条件。您可以使用类似这样的 mod_rewrite 指令来模拟它:

    RewriteCond %{REMOTE_ADDR} your_ip
    RewriteCond -d
    RewriteRule (.*)/$ $1/index.html
    

    如果您想排除网站的其他访问者查看 index.html,那么也可以使用

    RewriteCond %{REMOTE_ADDR} !your_ip
    RewriteRule (.*)/index.html$ $1/index.php
    

    【讨论】:

    • 是的,如果不可能的话,那将是我的后备方案。如果没有其他人有另一种解决方案,我会接受。
    猜你喜欢
    • 1970-01-01
    • 2018-02-07
    • 2023-03-22
    • 1970-01-01
    • 2015-12-24
    • 2017-01-07
    • 1970-01-01
    • 2011-11-08
    • 2013-10-27
    相关资源
    最近更新 更多