【问题标题】:Redirect JSON index files in sub dirs to PHP controller将子目录中的 JSON 索引文件重定向到 PHP 控制器
【发布时间】:2012-07-18 00:03:31
【问题描述】:

我正在尝试找出一种方法来制作它,以便如果我有一个包含 JSON 文件作为其索引的目录(例如index.json),我可以拥有它,以便当有人访问该目录时@ 987654322@ 被传递到该目录之外的“控制器”PHP 文件,该文件解码 JSON 并基于它生成页面。这是否可以通过 .htaccess 和/或 PHP 以某种方式实现,而无需拥有“控制器”PHP 文件的多个副本?

【问题讨论】:

    标签: php json .htaccess redirect url-rewriting


    【解决方案1】:

    您可以尝试使用 mod_rewrite 执行类似的操作。假设你的 php 控制器在这里:/path/to/json.php,你会使用这些规则

    RewriteEngine On
    
    # Check if request is for a directory
    RewriteCond %{REQUEST_FILENAME} -d
    
    # Check if there's an index.json file in that directory
    RewriteCond %{REQUEST_FILENAME}/index.json -f
    
    # Pass the request to the controller
    RewriteRule ^(.*)$ /path/to/json.php?file=$1/index.json [L]
    

    可以调整规则本身以适应控制器的工作方式。如果您的 json.php 控制器可以查看请求的 URI 并自行查找 index.json 文件,则您根本无法传递查询字符串。

    【讨论】:

    • 解决方案是完美的——简单而强大。我测试了它并且它有效。泰!是否可以让它在内部像这样解析,但将原始目录维护为地址栏中的可见 URL?
    • 只要路径是相对的,它就会将 orig dir 维护为可见 URL。再次——这是王牌。
    猜你喜欢
    • 1970-01-01
    • 2015-11-04
    • 2021-04-23
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多