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.
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);