【问题标题】:Redirect access to .htaccess to another file将对 .htaccess 的访问重定向到另一个文件
【发布时间】:2016-02-01 18:27:29
【问题描述】:

我正在玩弄 .htaccess 文件,完全出于好奇,我想知道是否可以将对 .htaccess 文件本身的访问重定向到另一个文件?

例子:

用户转到http://www.example.com/.htaccess

我希望将用户重定向到index.php?foo=bar

我尝试了以下方法:

Redirect 301 /.htaccess /index.php?foo=bar

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^((?s).*)$ index.php?foo=bar [QSA,L]
</IfModule>

这些方法适用于任何其他文件,但 .htaccess 文件本身总是会导致出现 403 Forbidden 页面。

这可能吗?如果不是,那为什么?

【问题讨论】:

    标签: apache .htaccess mod-rewrite redirect apache2


    【解决方案1】:

    如果您查看您的网络服务器配置文件,您会发现一些指令阻止用户从浏览器访问.htaccess.htpasswd 文件。例如,如果我打开位于 /etc/apache2/apache.conf 的 apache 配置,我可以发现以下几行:

    #
    # The following lines prevent .htaccess and .htpasswd files from being
    # viewed by Web clients.
    # 
    <FilesMatch "^\.ht">
        Require all denied
    </FilesMatch>
    

    所以你应该能够将上面的内容更改为:

    Order allow,deny
    Allow from all
    

    您还需要确保您对.htaccess 文件具有正确的文件权限,最后使用以下命令重新启动服务器:

    sudo service apache2 restart
    

    当然做上面的不会很安全。

    【讨论】:

    • 当然!现在看起来很明显,我正在看它。
    猜你喜欢
    • 1970-01-01
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多