Shortening urls using htaccess

I was looking for a solution for shortening urls for both blog and forums on my indie website I know there are a lot of shortening services on the web, for both hosted and self-hosted. Both have pros and cons. But the real questions were: Do I really want to send my data to someone else? Do I really want to use more hosting and database space for a self hosted app? Do I really want to keep this app updated?
The answer to all those questions was: NO. is a pretty short domain name, not 4 chars long only like Twitter’s, but short enough to keep his urls short with a little hack and the power of Apache’s mod_rewrite. This will still require a self hosted environment with .htaccess support and an installed cms that identifies its content by IDs (more details as you read). have a blog cms (WordPress, installed in root / folder) and a forum cms (phpBB3, installed in /forum/ folder), the tutorial splits in two parts, covering both. The same procedure may be adapted for any other cms.


On default settings WordPress access his contents (blog post, page, media) through their IDs like Even with seo friendly permalinks on, WordPress still allow to access posts by using ID, we will take advantage from this feature for our hack. uses Twenty Twelve child theme, this tutorial will assume you are using Twenty Twelve (or a child) but the procedure may be similar with other themes too.

First of we have to decide where to put this shortened link, in my case I went for the entry-meta footer that is shown after each post. This is done by twentytwelve_entry_meta() function found in theme’s functions.php, you can edit it from here (losing changes in case of theme update) or you can copy it in your child theme’s functions.php. An hook filter here would be cooler, but look like its the only way if you want the shorter link to show here.
After copying the whole stuff, add this single line right before the function’s trailing bracket:
printf('<a class="shorter-url" rel="nofollow" href="">Shorter Url</a>.', get_the_ID() );
This function will add the shortened link as, where %s is the post’s ID value we got from WordPress function get_the_ID(). (eg:

Done? Not yet. This only told WordPress how to shorten links using post IDs but it didn’t tell your hosting server how to handle those shortened urls.

Now open your favourite FTP program and navigate to your site’s root, here you will most likely find an .htaccess file, if not create it using your favourite text editor. Add this code to your file. If the file was not empty, make sure to put this code at top.
# BEGIN ShortUrls
<IfModule mod_rewrite.c>
RewriteEngine On
# END ShortUrls

(I will not bother you telling what all this stuff is about, a quick google search will tell you everything.)

Now, delete the entire line were I put TEXT GOES HERE, and paste this stuff inside
RewriteCond %{REQUEST_URI} ^/s/$
RewriteRule ^s/$ [L]
RewriteCond %{REQUEST_URI} ^/s/([0-9]+)$
RewriteRule ^s/([0-9]+)$$1 [L]

Let’s see what this code does: Read the line in pairs, basically they tell you if RewriteCond condition is met mod_rewrite will rewrite the url using the specified RewriteRule
Line #1 and #2:
Since the shortened url make use of the posts IDs, if this ID is missing (RewriteCond searches for “”), we only redirect the browser to the main site.
Line #3 and #4:
If the url ends with a number (eg:, take the numerical part ([0-9]+) after /s/ and point the browser to (eg:

Now we’re done.

Will the site loose his fancy SEO permalinks?
Absolutely no! WordPress will then take care of loading the correct post and, if permalink are enabled, printing the correct (longer) permalink in the browser address bar.

But this method only shortens the links by 1 char compared to ?p=ID method! I know, and are respectively 15 and 16 chars long. But this is the case only when wordpress is on root. When it is installed on /blog/ or, worse, /wordpress/ folder, the difference between and becomes obvious.
Read the phpBB3 part to better understand what I mean.


In my current setup PhpBB3 runs from a subfolder /forum/ and it shows post through a php page viewtopic.php?p=ID, making the full url very long Having this url short as would be awesome!

Like we did with WordPress we have to show those shortened ID somewhere in the forums. The best place to put this link is inside viewtopic_body.html, right before or after the topic title. In most themes you can find a line similar to this one
(Note: you don’t have to scan for the entire document, it is usually found in the first 5 lines)
<a href="{U_VIEW_TOPIC}">{TOPIC_TITLE}</a>

Next to this that you can paste something like:
<a class="shortlink" rel="nofollow" href="{S_TOPIC_ID}">Shorter Url</a>
This will make the post link much shorter, will not work on his own.
Get back to your .htaccess file and paste this code, right after the previous Rewrite codes.
RewriteCond %{REQUEST_URI} ^/f/$
RewriteRule ^f/$ [L]
RewriteCond %{REQUEST_URI} ^/f/([0-9]+)$
RewriteRule ^f/([0-9]+)$$1 [L]

Like previous codes
Line #1 and #2:
Will tell browsers to load the forum main page when the browser is trying to load a shortlink with no id.
Line #3 and #4:
Will send browsers to the correct link

Here you can see a big difference: is 34 characters long, while is only 14, 20 characters less.

Do I have to use and No, you can use whatever you want like b/ or s-, just make sure your RewriteConds and RewriteRules use the correct regular expressions.

5 thoughts on “Shortening urls using htaccess

  1. Naked Teen Girls Looking for Sex:

    Popular tags: jack mormon dating site, hook up hookah lake forest, close christian dating for free account, together dating service mechanicsburg pa, online dating sites mobile, online dating sites bahrain, list of common dating sites, 1 year online dating at 50, single parent dating philippines, dating kramer guitars, short dating before marriage, sparks network dating sites, china hookup, world 100 free dating site, best hookup places in dubai, dating native american indian, jerma tf2 matchmaking, red rose radio dating, muslim dating sites in dubai, speed dating stevens point wi, dads dating daughters, lethbridge dating free, straight dating app android, free wordpress dating template, free chicago dating website, tattoo dating sites australia, badoo free online dating, dating dayton, rv water hookup leaking, top 5 lesbian dating sites, polish dating wroclaw, spirit filled christian dating site, hook up with someone out of their league, hook up delta wye transformer, web affair dating site, killer dating profile, dating in windsor ontario, toll free dating services, australia best free dating sites, 40’s dating, speed dating south east london, dating herpes free, tips for dating a younger guy, dating agencies leeds west yorkshire, online dating market in china, dating site for single ladies, dating someone with the same name as your mother, advantages and disadvantages of casual dating, instant dating london, pros and cons of internet dating sites, gay dating websites in thailand, best online dating sites list, oldest dating youngest, free online dating sites in zambia, dating in virginia, gay dating in tulsa, what is love dating and courtship, middle eastern dating site, age difference dating calculator, speed dating hesperia ca, mary kate and ashley dating life, dating bridge and tunnel, online dating strategy, dating sites market harborough, barcelona hook up bars, ny hook up, dating site, no registration dating uk, baggage dating a serial killer, northern va speed dating, dating site username advice, 343 matchmaking update, fraser valley dating site, where to hook up vacuum gauge, do photographers hook up with models, location based dating website, free dating vancouver bc, free casual dating test, is dating worth it new yorker, fat dating app, 100 free dating site in austria, dating site christian, indian astrology match making free, hiv online dating sites, dating site we should, speed dating saskatoon sk, dating someone who is newly divorced, be2 dating site reviews, online dating erstes treffen, speed dating zlin, carey mulligan dating history, dating app for transsexuals, chat rooms online dating, benefits of dating me meme, australia older dating, singles dating northern ireland, song lyrics about online dating, dating websites t, cape town dating free, lav dating site, bf3 co op matchmaking, dating oekraine, minot speed dating, old dating habits, marathi dating sites in india, aust online dating, free online dating saskatchewan, how to stop dating ads on youtube, nairobi sugar mummies dating sites, asian dating venture malaysia, dating someone you are not in love with, dating adventures games, dating in fort myers florida, dating a pisces guy, online dating site price comparison, online dating profile about me examples, best overseas dating sites, veterinary dating site, quotes for online dating, speed dating nijmegen, dating online nederland, safe dating slogans, white guys dating hispanic, golfers dating sites, pot smoker dating, dating a serial dater, how to create the perfect online dating profile, dating a divorced woman red flags, cosmo best dating sites, internet dating can be harmful, malaysia indian matchmaking, online dating nz christian, doctor who fan dating site, dating vintage cars, letting it go dating reviews, started dating late, dating texting anxiety, dating to becoming exclusive, free dating in hyderabad india, engineering dating website, lpga dating rumors, 22 year old dating 29 year old, questions to ask a man before dating him, match dating event, online dating sugar mamas, cs go matchmaking servers bad, dating in glasgow, dating free russian, funniest online dating emails, icebreaker emails online dating, isotopic dating, hk dating places, chennai dating girl contact, online dating whatsapp numbers, 100 percent free online dating sites, free dating sites no cost at all, dating a latino, dating better looking guy, private dating service nj, best desi dating app in usa, speed dating devon cornwall, thaimate dating, aruba dating sites, sex after 1 year of dating, why dating rules don’t work, fs dating, how to write a self summary for dating site, free philadelphia dating sites, lesbian christian dating uk, dating pregnant site, morehead dating, can dating ruin friendship, online dating after break up, dating swords, dating love-romance games, free christian dating new zealand, series dating rules, best online dating single parents, single solution dating, advantages of dating your best friend, dating nagpur, dating vintage lee jeans, indian dating sites wiki, alphabet dating letter g, matchmaking services santa barbara, athletic singles dating sites canada, free hookup sites for singles uk, dating uden registrering, russian blacklist dating scams, one year online dating at 50, interracial dating asian white, online dating free trials, speed dating baselland, free membership online dating site, dating shows list, dating tips for christian ladies, best place to hook up in atlantic city, dating ghanaian men, dota 2 bot match making, rv hook up 30 amp outlet, anastasia dating agency, cerbung rify matchmaking part 15, sexiest online dating profiles, lesbian dating website nyc, youtube poop hyrule dating videos, 4. what is the difference between relative dating and radiometric dating, free online dating in canada ontario, best hookup bars, great opening lines for dating websites, mono dating poly, online dating rituals of the american male cast brian, christian online dating sites in nigeria, dating a feisty woman, daughter dating wrong guy, dating pregnancy ivf, scorpio dating a virgo man, lee thompson young dating history, dating website houston tx, dating asexual guy, free dating sites for alaska, warning signs when dating a guy, den danske mafia-speed dating, speed dating in atlanta, art dating websites, dating a ex girlfriend’s friend, std dating new zealand, south korean girl dating site, online dating college educated, delhi dating online, rules for dating my teenage daughter wiki, signs guys only want to hook up, gratis dating tjeneste, what should my dating headline be, dating yourself definition, yorkshire dating agency, top ten dating sites for free, am i getting blown off dating, who is the woman dating billy in eastenders, best dating app in asia, dating agency cyrano ep 16 recap, making a online dating profile, dating 4 years not living together, arthritis dating site, is dating a family friend bad, tokyo dating club, pairs skating dating, speed dating all bar one brighton, florence sc dating, free dating no credit cards needed, red deer speed dating, good headlines to use on dating sites, the hook up tackle arizona, dating man with no car

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.