setting up htaccess redirects to wordpress post id

Posted on 2010-04-10
Medium Priority
Last Modified: 2012-05-09
I'm trying to set up an automated link shortener using htaccess, in which, for example:


.. will redirect to:


Here's how I set it up; it's not working:

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^shorturl.com/u?$ [nc]
rewriterule ^(.*)$ http://longurl.com/?p=$1 [r=301,nc]

Do I need to use QUERY_STRING or something? Can someone straighten me out?


Question by:nbierm65
  • 4
  • 3

Accepted Solution

subscript earned 375 total points
ID: 30323374
When you say shorturl.com and longurl.com in the paths -- are these two different domains or... just a longer and shorter version of the same one?

Either way, this command should work for you, replace yours with this:

RewriteEngine On
RewriteBase /
RewriteRule ^u/([0-9]+)/? http://www.longurl.com/?id=$1 [L]

Does that work for you?

If permanently forwarding traffic to a new domain -- a 301 redirect is more search engine friendly.


Author Comment

ID: 30326558
didn't work. They're two different domains. The second uses WordPress, which uses ?p=ID instead of ?id=ID -- don't know if that makes a difference..

Expert Comment

ID: 30330719
Oops, my mistake -- yes:   Change it to p= instead.

Does it work then?
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.


Author Comment

ID: 30332358
no, still doesn't :(  The WordPress codex has this line of code for rewriting long urls; is this something I should look at:

 RewriteRule ^post/([0-9]+)?/?([0-9]+)?/?$ /index.php?p=$1&page=$2 [QSA]

Assisted Solution

subscript earned 375 total points
ID: 30335000
Huh, I guess I'm not sure I understand clearly what's happening.

So... does the redirect script work as written?

In other words -- if you visit the "shorturl.com/u/123" does it attempt to load the longurl.com address you gave it?

Or is the problem that the URL you're redirecting to doesn't work?

If you want to test to be sure -- send it to http://www.yahoo.com/?p=$1 instead.

At that point -- at least you know that the redirect script is working correctly. Then you just have to focus on finding the right URL structure to send to on the longurl.com domain... right?

Or am I missing something?
Let me know.


Author Comment

ID: 30429789
OK, I got my original attempt to work by removing the path to the subdirectory and installing the htaccess file directly in the subdirectory:

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^shorturl.com?$ [nc]
rewriterule ^(.*)$ http://www.longurl.com/?p=$1 [r=301,nc]

Open in new window


Author Closing Comment

ID: 31761062
partial solution

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses
Course of the Month5 days, 1 hour left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question