Skip

Short URL service in the same directory as WordPress?
June 15, 2011 2:27 PM   Subscribe

How do I create a custom bit.ly-like short URL service in the same directory as a WordPress installation?

I have an existing WordPress 3.0 installation for my blog. The current URL structure for individual posts is something like this: http://example.com/2011/post-title-goes-here

I would like to keep this current structure, but also use my domain (without subdirectories or subdomains) as a bit.ly-like "short" URL service where I can shorten not only my own content, but links to other content as well.

So, for example, the URL I posted above would work, but I want something like this to work as well: http://example.com/OC469ei

Right now I have a YOURLS installation in a subdirectory that works, so my links are something like this: http://example.com/s/OC469ei

But I would really like to do away with the subdomain if at all possible. YOURLS claims specifically that it cannot be installed in the same directory as WordPress because they both need .htaccess files.

I'm happy to use whatever method necessary to make this work, even if it's a manual process that's not as pretty as the YOURLS admin interface.

Please let me know if there's anything I need to clarify.
posted by joshrholloway to Computers & Internet (5 answers total) 2 users marked this as a favorite
 
What you will need to do is write the appropriate mod_rewrite regular expression that can distinguish between a wordpress post URL and a shortened link URL and rewrite the URL to its canonical form. This means modifying the existing .htaccess file that wordpress uses. You'll need to make sure that under whatever scheme you use for link shortening that the URLs can never be confused, for example it looks like you currently have /[A-Za-z0-9]{6}/ and /20[0-9]{2}/ so that's probably sufficient.
posted by Rhomboid at 2:56 PM on June 15, 2011


Rhomboid is dead on. Mod-rewrite may frustrate you a bit, but it's the best solution for this.
posted by bprater at 3:02 PM on June 15, 2011


I cheat this a little bit by putting all my content at www.example.com, and having my URL shortener at example.com. In that example.com's VirtualHost section, <Location />...</Location> I use RewriteEngine:
RewriteEngine on
RewriteRule htdocs/$ http://www.example.com/
RewriteRule htdocs/(..*)$ http://www.example.com/cgi-bin/tinylink.pl?r=$1

tinylink.pl checks to see if it's got the URL in its tied hash, and if it doesn't then it just redirects to http://www.example.com/$1

Thus http://example.com/A_Valid_Document redirects to http://www.example.com/A_Valid_Document, and http://example.com/ljsfa gets matched by tinylink.pl and redirected to the appropriate place.
posted by straw at 3:10 PM on June 15, 2011


So a little more thinking... How about:

RewriteRule htdocs/([A-Za-z0-9]+)$ http://example.com/s/$1

This would take anything in your main directory that consisted only of upper and lower case letters and numbers, and redirect it to your shortener. That way you can still have http://example.com/2011/... and http://example.com/page.html go where they should, but http://example.com/OC469ei will redirect to your shortener.

I'm not sure how to rewrite that RewriteRule to fit into the .htaccess rather than the Apache site config, but it shouldn't be too hard.
posted by straw at 4:42 PM on June 15, 2011


This WordPress plugin may help (it appears to have been abandoned but it's worth a shot).
posted by cvp at 5:47 PM on June 15, 2011


« Older Help! I am in need of a physic...   |  Is there an app/webapp that wi... Newer »
This thread is closed to new comments.


Post