Skip to content

Instantly share code, notes, and snippets.

@damianopetrungaro
Last active October 23, 2016 20:28
Show Gist options
  • Save damianopetrungaro/2b1a0a7b55e2ff2e08b8cac4467676c2 to your computer and use it in GitHub Desktop.
Save damianopetrungaro/2b1a0a7b55e2ff2e08b8cac4467676c2 to your computer and use it in GitHub Desktop.
SEO4AJAX
### Activate the rewrite rule engine
RewriteEngine on
### If the request contains the _escaped_fragment_ query parameter, set a flag to proxify the request to SEO4Ajax
RewriteCond %{ENV:PROXIFY} !true
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} _escaped_fragment_= [NC]
RewriteRule .* - [E=PROXIFY:true,E=REQUEST_PATH:%{REQUEST_URI}]
### If the request is issued by another known bot, set a flag to proxify the request to SEO4Ajax
### Note: the 5 following lines can be commented if you are using #! URLs
RewriteCond %{ENV:PROXIFY} !true
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_USER_AGENT} !(googlebot|yandexbot|pinterest.*ios|mail\.ru|seznambot|screaming) [NC]
RewriteCond %{HTTP_USER_AGENT} (bot|spider|pinterest|crawler|archiver|flipboardproxy|mediapartners|facebookexternalhit|quora|WhatsApp) [NC]
RewriteRule .* - [E=PROXIFY:true,E=REQUEST_PATH:%{REQUEST_URI}]
### Proxify the request to SEO4Ajax
RewriteCond %{ENV:PROXIFY} true
RewriteRule ^(.*)$ /CUSTOM_PHP_PAGE_HERE.php [QSA,L]
### Activate the rewrite rule engine
RewriteEngine On
### If the request contains the _escaped_fragment_ query parameter, set a flag to proxify the request to SEO4Ajax
RewriteCond %{ENV:PROXIFY} !true
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} _escaped_fragment_= [NC]
RewriteRule .* - [E=PROXIFY:true,E=REQUEST_PATH:%{REQUEST_URI}]
### If the request is issued by another known bot, set a flag to proxify the request to SEO4Ajax
### Note: the 5 following lines can be commented if you are using #! URLs
RewriteCond %{ENV:PROXIFY} !true
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_USER_AGENT} !(googlebot/|googlebot-mobile|yandexbot|pinterest.*ios|mail\.ru|seznambot|screaming) [NC]
RewriteCond %{HTTP_USER_AGENT} (bot|spider|pinterest|crawler|archiver|flipboardproxy|mediapartners|facebookexternalhit|quora|WhatsApp) [NC]
RewriteRule .* - [E=PROXIFY:true,E=REQUEST_PATH:%{REQUEST_URI}]
### Proxify the request to SEO4Ajax
RewriteCond %{ENV:PROXIFY} true
RewriteRule ^(.*)$ http://api.seo4ajax.com/YOURAPIKEYGOESHERE%{ENV:REQUEST_PATH} [P,QSA,L]
### Otherwise, if the requested file is not present on the server, serve the index file
### Note: the 3 following lines can be commented if you are using #! URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.html [L]
<?php
//Configuration
$s4aToken = 'YOUR CUSTOM KEY';
$s4aHost = 'http://api.seo4ajax.com/';
function x_forwarded_for() {
$ip = '';
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
function parse_request_headers() {
$requestHeadersArray = array();
$requestHeaders = '';
foreach($_SERVER as $key => $value) {
if(substr($key, 0, 5) == 'HTTP_') {
$requestHeadersArray[str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))))] = $value;
}
}
foreach($requestHeadersArray as $key => $value) {
if($key != "Host" && $key != "Cookie" && $key != "X-Forwarded-For" && $key != "Connection") {
$requestHeaders .= $key . ":" . $value . "\r\n";
}
}
$requestHeaders .= 'X-Forwarded-For: '. x_forwarded_for() . '\r\n';
$requestHeaders .= 'Connection: close\r\n';
return $requestHeaders;
}
$opts = array(
'http'=>array(
'method'=>'GET',
'header'=> parse_request_headers(),
'follow_location' => false,
'ignore_errors' => true
)
);
$context = stream_context_create($opts);
$url = $s4aHost . $s4aToken . $_SERVER['REQUEST_URI'];
$result = file_get_contents($url, false, $context);
if ($result !== FALSE) {
foreach($http_response_header as $headerLine) {
header($headerLine);
}
echo $result;
}
else
{
$protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
header($protocol . ' 500 Internal Server Error');
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment