Skip to content

Instantly share code, notes, and snippets.

@filipbech
Last active September 22, 2015 08:24
Show Gist options
  • Save filipbech/51077b076aa3f330e735 to your computer and use it in GitHub Desktop.
Save filipbech/51077b076aa3f330e735 to your computer and use it in GitHub Desktop.
Htaccess+php stuff for api redirect (for local development without xss issues)
.htaccess
RewriteEngine on
RewriteRule ^umbraco/(.+?)$ umbraco.php?url=$1 [QSA,L]
## Nedenstående er til spa-ting
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ /index.html
umbraco.php
<?php
$method = getenv('REQUEST_METHOD');
$prefix='umbraco/';
$domain = 'http://5065aa.abjerner/';
$urlvars = $_GET;
unset($urlvars['url']);
function sådander($options) {
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$response = (object) array(
'header' => substr($response, 0, $header_size),
'body' => substr( $response, $header_size),
'http_code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
'content_type' => curl_getinfo($ch, CURLINFO_CONTENT_TYPE)
);
curl_close($ch);
return $response;
}
function get($url, $query) {
$url = $url . '?' . http_build_query($query);
$options = array(
CURLOPT_HEADER => 1,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1
);
return sådander($options);
}
function post($url, $query, $post) {
$url = $url . '?' . http_build_query($query);
$options = array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 1,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POSTFIELDS => http_build_query($post)
);
return sådander($options);
}
if ($method == 'GET') {
$response = get($domain . $prefix . $_GET['url'], $urlvars);
if ($response->content_type) {
header('Content-type: ' . $response->content_type);
}
echo $response->body;
exit;
} else if ($method == 'POST') {
$response = post($domain . $prefix . $_GET['url'], $urlvars, $_POST);
if ($response->content_type) {
header('Content-type: ' . $response->content_type);
}
echo $response->body;
exit;
}
exit('Hvad har du gang i?');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment