【问题标题】:Can htaccess redirect to parent folder?htaccess 可以重定向到父文件夹吗?
【发布时间】:2021-06-29 13:17:42
【问题描述】:

我的域在我的用户文件夹中。我想在同一用户下提供另一个文件夹中的文件。我试过这个:

RewriteRule ^test$ ../testfolder/test.txt [L]

但是 Apache 提供 400 Bad Request。 htaccess 可以降级吗?

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite


    【解决方案1】:

    您可以使用此代码动态找出PARENT 目录,然后重写为:

    RewriteEngine On
    
    # dynamically figure out parent directory using composite RewriteCond
    RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)/[^/]+/\1$
    RewriteRule .* - [E=PARENT:%2]
    
    RewriteRule ^test$ %{ENV:PARENT}/test.txt [L,NC]
    

    RegEx Demo

    【讨论】:

    • anubhava 我读到 htaccess 不会重定向到文档根目录之外,所以我只是在 php 脚本中使用了 chdir() 。不过谢谢你的回复,看起来很聪明。
    • 这根本不是重定向到外部文档根目录。它用于从/project/usr/test 重写为/project/test.txt 其中/project 是站点根目录的子目录
    猜你喜欢
    • 2016-08-30
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    • 2014-06-01
    相关资源
    最近更新 更多