Jacob Warren
Jacob Warren

Reputation: 21

Running Tumblr on Subdirectory almost successful

I have been working on a tiny proxy to run a Tumblr blog from a subdirectory. I have been able to mask the Tumblog to example.com/blog/, but I can't follow any of the links in the blog. I'm a designer and have spent a good part of the day researching this. Sorry if this sounds stupid to you all. My question is why aren't the links in the blog working?

Here's my .htaccess:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog\/?$ proxy.php?url=http://seo-services-greece.tumblr.com/$1?%

<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresByType image/gif A2592000
    ExpiresByType image/jpeg A2592000
    ExpiresByType image/png A2592000
    ExpiresByType text/css A2592000
    ExpiresByType application/javascript A2592000
    #ExpiresDefault A2592000
</IfModule>

AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE application/x-httpd-eruby
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE text/ecmascript
AddOutputFilterByType DEFLATE application/ecmascript
AddOutputFilterByType DEFLATE text/jscript

Header unset ETag
FileETag None

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^seoprojects.gr [nc]
rewriterule ^(.*)$ http://www.seoprojects.gr/$1 [r=301,nc]

Here's my proxy.php:

<?php 

$from = "seo-services-greece.tumblr.com";
$unto = "seoprojects.gr/blog";

// Because Dreamhost doesn't do remote fopens, and to get content-type
function fetch($url) {
    $curl = curl_init();
    $timeout = 5; // set to zero for no timeout
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    $html = curl_exec($curl);
    $content_type = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
    curl_close($curl);
    return array($html, $content_type);
}

list($html, $content_type) = fetch($_GET['url']);

// Fix root-relative links etc.
$html = preg_replace('/\b(href|src|rel)="\//', '$1="http://'.$unto.'/', $html);

// Fix the iframe-url
$html = str_replace("iframe?src=http://".$unto, "iframe?src=http://".$from, $html);

// fix audioplayer-url
$html = str_replace("audio_file=http://".$unto, "audio_file=http://".$from, $html);

// Replace the old URL with the new
$html = str_replace($from, $unto, $html);

header("Content-type: $content_type");
echo $html;

?>

Upvotes: 2

Views: 1293

Answers (1)

LazyOne
LazyOne

Reputation: 165471

Your rewrite rule (the pattern) is incorrect -- you have back reference $1 but have no "catch group" that will be used for $1. Use these lines instead:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/?$ proxy.php?url=http://seo-services-greece.tumblr.com/ [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/(.+)$ proxy.php?url=http://seo-services-greece.tumblr.com/$1 [L]

First rule will deal with /blog and /blog/ while second will work with /blog/something.

Unfortunately any query string will be lost (e.g. /blog/?ref=yahoo). To pass it to proxy.php as well, try adding ?%{QUERY_STRING} at the end of target URL: e.g. proxy.php?url=http://seo-services-greece.tumblr.com/$1?%{QUERY_STRING} -- I just do not guarantee that this will work just like that -- it may require some escaping.


UPDATE: You have 2 rewrite rules blocks -- one on very top and one at very end of .htaccess. You need to move those from the bottom yo the very top and place them before proxy.php rules:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

# force www. in domain name
RewriteCond %{HTTP_HOST} ^seoprojects.gr [NC]
RewriteRule ^(.*)$ http://www.seoprojects.gr/$1 [R=301,L]

# thumblr proxy rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/?$ proxy.php?url=http://seo-services-greece.tumblr.com/ [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/(.+)$ proxy.php?url=http://seo-services-greece.tumblr.com/$1 [L]

Upvotes: 1

Related Questions