Have you ever wanted to have short links (example.com/123) to your blog post, so the links are not cut off in social media posts or emails?

Over the years we came up with the following Apache rewrite rules that you can insert into your .htaccess file.
As always backup first!

When you add them you can have example.com/123 and this will redirect to the full SEO friendly link to your WordPress blog post.

You can find the post id in the address bar when writing the blog post. The address bar contains this /wp-admin/post.php?post=7604&action=edit

Additional benefits.

Twitter
example.com/twitter7604 => will expand and add Google Analytics utm parameters and say that the link came from twitter.

Facebook
example.com/fb7604 => will expand and add Google Analytics utm parameters and say that the link came from facebook.

YouTube
example.com/yt7604-vx2u5uUu3DE -> you can insert this link and it will redirect to the post and then append the video ID so you'd know which youtube video send traffic to a given blog post page.

We like to put the link first in the description box and then follow with the description of the video. That way the link is visible as YouTube cuts off the description at around 50-60 characters and adds a link/button to view the full description.

###################################################################
# Orbisius Blog redirects for Apache web server
# Blog post - https://orbisius.com/7604
<IfModule rewrite_module>
        RewriteEngine On
        
        # you may need to update this if used in a folder.
        RewriteBase /

	# When 123 or p123 redirect to the blog post
	# NC -> case insensitive
	# QSA - query string append
	RewriteRule ^/?p?[\-\_]*(\d+)$/? /?p=$1&utm_source=shortlink&utm_medium=blog_post&utm_content=post$1&utm_campaign=content_mkt [L,R=302,QSA,NC]

	# link to a blog post and also provide video ref
	# https://orbisius.com/yt4659-asf872akjas872
	RewriteRule ^/?((?:yt|youtube|reddit|twitter|fb|facebook|insta|instagram|pinterest|linkedin|tiktok)|[yfgtli]?|[a-zA-Z\-\_]+)[\-\_]*(\d+)[-_]([\w\-]+)$/? /?p=$2&utm_source=$1&utm_medium=ref-$3&utm_content=post$2&utm_campaign=content_mkt [L,R=302,QSA,NC]

	# When y123 redirect to the blog post but add source that it's youtube.
	RewriteRule ^/?((?:yt|youtube|reddit|twitter|fb|facebook|insta|instagram|pinterest|linkedin|tiktok)|[yfgtli]?|[a-zA-Z\-\_]+)[\-\_]*(\d+)$/? /?p=$2&utm_source=$1&utm_medium=$1&utm_content=post$2&utm_campaign=content_mkt [L,R=302,QSA,NC]

	# Enables me to have a short link that expands so we can tell where people are coming from
	# sometimes youtube links start with - but it's rare you still need to have yt- prefix and then video id.
	# e.g. http://orbisius.com/yt
	# e.g. http://orbisius.com/yt-MNOsldDS_pY
	RewriteRule ^/?(?:yt?|youtube)[\-\_]?([\w\-]*)$/? /?utm_source=youtube&utm_medium=social&utm_content=$1&utm_campaign=video_mkt [L,R=302,QSA,NC]
</IfModule>
###################################################################

The following rules assume that you have WordPress installed in your root (/) folder.

Running WordPress in a folder e.g. /site

This is the updated version of the .htaccess rules. Feel free to replace /site in all places if you're using a different folder for WordPress.

###################################################################
# Orbisius Blog redirects for Apache web server
# Blog post - https://orbisius.com/7604
<IfModule rewrite_module>
    RewriteEngine On

    # you may need to update this if used in a folder.
    # /projects/apps/app and need to prepend the web path to all the rules below /?p=
    RewriteBase /site

    # When 123 or p123 redirect to the blog post
    # NC -> case insensitive
    # QSA - query string append
    RewriteRule ^/?p?[\-\_]*(\d+)$/? /site/?p=$1&utm_source=shortlink&utm_medium=blog_post&utm_content=post$1&utm_campaign=content_mkt [L,R=302,QSA,NC]

    # link to a blog post and also provide video ref
    # https://orbisius.com/yt4659-asf872akjas872
    RewriteRule ^/?((?:yt|youtube|reddit|twitter|fb|facebook|insta|instagram|pinterest|linkedin|tiktok)|[yfgtli]?|[a-zA-Z\-\_]+)[\-\_]*(\d+)[-_]([\w\-]+)$/? /site/?p=$2&utm_source=$1&utm_medium=ref-$3&utm_content=post$2&utm_campaign=content_mkt [L,R=302,QSA,NC]

    # When y123 redirect to the blog post but add source that it's youtube.
    RewriteRule ^/?((?:yt|youtube|reddit|twitter|fb|facebook|insta|instagram|pinterest|linkedin|tiktok)|[yfgtli]?|[a-zA-Z\-\_]+)[\-\_]*(\d+)$/? /site/?p=$2&utm_source=$1&utm_medium=$1&utm_content=post$2&utm_campaign=content_mkt [L,R=302,QSA,NC]

    # Enables me to have a short link that expands so we can tell where people are coming from
    # sometimes youtube links start with - but it's rare you still need to have yt- prefix and then video id.
    # e.g. http://orbisius.com/yt
    # e.g. http://orbisius.com/yt-MNOsldDS_pY
    RewriteRule ^/?(?:yt?|youtube)[\-\_]?([\w\-]*)$/? /site/?utm_source=youtube&utm_medium=social&utm_content=$1&utm_campaign=video_mkt [L,R=302,QSA,NC]
</IfModule>
###################################################################
Referral Note: When you purchase through a referral link (if any) on this page, we may earn a commission.
If you're feeling thankful, you can buy me a coffee or a beer