WordPress Tilde Hack for Home Directories

While browsing through the preferences of NetNewsWire, I noticed in the preferences there was a way to blog a entry of an RSS feed. To do this, the application shelled out to another application to do the heavy lifting.

That application was MarsEdit, a tool that was supposed to make blogging as easy as writing an email.

Problem was, when I went to open my blog in MarsEdit, I ran into a bit of a problem. MarsEdit was inserting %7E in the url, which is obviously the safe hex representation for the tilde sign. (Note, it’s tilde, with an ‘e’, not tilda.)

Look at your web browser’s URL for just a second. You should see something that looks like this: http://www.wwco.com/~wls/blog/

The tilde is a short hand notation that says to use my home directory. The default install of Apache allows this, because user home pages are in physically separate locations from the actual site’s webpages.

MarsEdit was trying to do the safe thing, by encoding something that should always work. And, Apache did the right thing by going to the right web page. Problem is, WordPress does the wrong thing — it reads the URL as-is and doesn’t realize %7E is the same as ~.

MarsEdit is not the only application that does this, many others do: it is the correct behavior. Even links from Digg, will do this on occasion.

I failed to find a decent solution to fix the problem, too. Discussions on the WordNet site seemed to ignore the fact that this was a problem, pointing people to Apache’s pages. Solutions that worked for others, didn’t work for me. Remember, Apache was delivering content, specifically WordPress content, and WordPress couldn’t deduce the entry to show, so it showed it’s own 404. This further supports the problem being WordPress’s.

I tried some mod_rewrite tricks, and those didn’t work. I even tried muddling inside the functions of WordPress, but it seemed that no matter where I made my changes, they either didn’t take or something broke. The page selection code was happening far to upstream, and I was getting bitten by it.

So I resorted to the final hack I knew would work. All WordPress requests go to the index.php file to start with, and it is Apache’s REQUEST_URI which is correctly preserving the encoded string. So, I figured before any other script of function could get its hands on it, I’d change that string.

Inside the <?php?> tags, I added this one line:
$_SERVER['REQUEST_URI'] = preg_replace( "/%7[Ee]/", "~", $_SERVER['REQUEST_URI'] );

This simply substitutes the %7E back into a tilde, so WordPress gets a familiar string to work with.

This solved my problem instantly. It’s ugly, but it works.

Please if you suffer from this problem because you’re using WordPress in your home directory, make a little notice to the authors, but while you’re at it, express some gratitude too at what a nice system they’ve made.

This entry was posted in Bug Report, How To, Programming, Rant, Software, Web Places. Bookmark the permalink.

4 Responses to WordPress Tilde Hack for Home Directories

  1. Douglas says:

    I tried this solution with version 2.6 of WP to no avail. Do you know if there is somewhere different that we should be placing that line of code?

  2. Douglas says:

    Also, someone might find this useful. http://trac.wordpress.org/ticket/7270

  3. EEWaver says:

    Douglas,

    Instead of adding the line to index.php, add it to wp-settings.php and your problems should be solved. I added it right before the line that says: “// Fix for IIS, which doesn’t set REQUEST_URI”

  4. Your solution about putting it on the wp-settings.php file worked GREAT. Thank you; hope this proves useful to others as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>