doufu6423 2018-01-31 18:59
浏览 45
已采纳

基于调用资产的html文件URL传递资源

If i have an html file located at http://sample.com and that html file is loading in an asset with an ajax call, can the server intercept that asset and deliver a different asset without the html file changing the url or adding url parameters to the asset ur;?

example: http://sample.com/index.html?configID=567 has a config.json file being loaded with ajax like this: $.getJSON('config.json', function(data){});

can apache know that this config.json file is being requested by an html file that has a url parameter of configID=567 and pass back a response from config567.json instead of config.json?

I can't modify the javascript in the html file to read $.getJSON('config.json?configID=567', function(data){}); so I would like to just do the modifications on the server side.

I also have php running as well if there is a php solution for this without changing the html file to a php file.

  • 写回答

1条回答 默认 最新

  • duandianzhong8315 2018-01-31 19:11
    关注

    You seem to be looking for Referer-based Rewrite in Apache. The implementation details may differ for you, but you can try this to get different files depending on the ID:

    RewriteEngine On
    RewriteCond %{HTTP_REFERER} configID=(\d+)$
    RewriteRule ^config.json$ configs/config-%1.json
    

    The %1 part refers to the parenthesized group in the RewriteCond line.

    Alternatively, you can enable PHP-processing for json file type and implement the logic in the php:

    // config.json
    <?php
    $id = $_GET['configID'];
    header('Content-Type: application/json');
    echo get_config_from_database_by_id($id);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式