The trials and tribulations of moving to /blog

Link to the updated version of this post

I decided before I put my website back up that I wanted to switch from using subdomains to using subdirectories (faq.elletricity.com to elletricity.com/faq). When you switch your blog over to a custom domain, Tumblr takes over all subdirectories so that you can create custom pages on your customize page. If you want to have your own website content that is not controlled by Tumblr, you must create subdomains via your hosting provider.

This is fine if you’re not doing much with your website, but I wanted to use PHP and upload my own files to those directories, and making a subdomain for each page was getting tedious. So I attempted to change my custom domain to http://elletricity.com/blog. BUT, Tumblr doesn’t allow you to put backslashes in your custom domain name. I was stumped.

After some time Googling the problem, I discovered a sort of “hack" to make this work. I created a subdirectory called /blog in which I had two files:

proxy.php

<?php

$from = "elletricity .tumblr.com";
$unto = "elletricity.com/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);

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

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

?>

and .htaccess

RewriteEngine On
RewriteBase /blog

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$  proxy.php?url=http://elletricity .tumblr.com/$1

I also included a script at the top of my theme:

<script type="text/javascript">
if (location.host == 'elletricity .tumblr.com')
  location.replace(location.href.replace('elletricity .tumblr.com', 'elletricity.com/blog'));
</script>

This seemed to work perfectly… and then I tried accessing my page. None of the plugins were working and only the dashboard button showed up - the follow button was missing. I discovered that the script rewrote all my URLs - including the ones for my Tumblr controls. Because http://elletricity.com/blog is not a valid Tumblr URL, Tumblr took away my follow button :( The permalink page controls (Like, Reblog) still worked, however.

(I put a space after elletricity in the scripts I pasted here, so that my script won’t change those, too!)

I ended up hiding the homepage controls through some CSS in my theme:

{block:IndexPage}<style>#tumblr_controls{display:none!important}</style>{/block:IndexPage}

I then attached the controls to an element dynamically in my Javascript file:

$("#control_fix").after("<iframe src='http://assets.tumblr.com/iframe.html?10&src=http%3A%2F%2F"+elle.name+".tumblr.com%2F&amp;lang=en_US&amp;name="+elle.name+"' scrolling='no' width='330' height='25' frameborder='0' style='position:absolute; z-index:1337; top:0px; right:0px; border:0px; background-color:transparent; overflow:hidden;' id='tumblr__controls'></iframe><!--[if IE]><script type='text/javascript'>document.getElementById('tumblr_controls').allowTransparency=true;</script><![endif]-->");;

And inserted one more script into my theme:

<script>var elle={name:'{Name}'}</script>

The variable was so I could do the same for my answers blog. So I now have http://elletricity.com/blog and http://elletricity.com/answers, complete with working Tumblr controls, to show for all my hard work.

Seems like a lot of superficial masking for one little change in URL, but sheer brute force and a lot of hair-tearing seemed to do the trick.

If anybody else is interested in this, I hope it helps you!

Newer Older