Right now my shortlink structure looks something like this:
example.com/?p=451
I would prefer it to look more like this:
example.com/abc123
Any ideas how I can alter the code to do just this?
Right now my shortlink structure looks something like this:
example.com/?p=451
I would prefer it to look more like this:
example.com/abc123
Any ideas how I can alter the code to do just this?
You must be logged in to post a comment.
Okay, so, as mentioned in my comment to you: altering core files is not a good idea. But here is a plugin solution.
First we’re going to create our own rewrite rules that rewrite
s/123
(replace 123 with a post ID) toindex.php?short=123
. We’ll also have to filter the WordPress query variables so we can use them later.The
add_rewrite_rule
call says “rewite s followed by a slash and a string of one or more digits toindex.php?short=the_string_of_digits
.Then we can hook into template redirect and see if our query variable is there. If it is, we’ll try and get a permalink out of it. If that fails, we’ll throw a 404 error. Otherwise, we’ll use `wp_redirect’ to send folks to the actual post.
Finally, we hook into
get_shortlink
to change how our rel=”shortlink” appears in the<head>
section of the site, and elsewhere. This new shortlink structure will reflect the rewrite rule we wrote above.As a plugin:
https://gist.github.com/1179555
Aside from @ChristopherDavis’s answer, you can also do it in a PHP independent way, using .htaccess. Simply add this rule:
Alternative without
mod_rewrite
, usingmod_alias
:The only problem is, two redirects happen here (instead of 1) â (for example) if user visits
http://example.com/s/121/
he is redirected by the web server tohttp://example.com/index.php?p=121/
and then by WordPress to the actual permalink of the post.The advantage is, this never breaks! Plugins may break, but this does not.
PS: I use this (short link structure would be
http://example.com/-121
where 121 is post ID):using Redirection plugin, you can add a redirect which achieves this without touching PHP or .htaccess.
prerequisites
add redirect
redirect parameters
this method has the following advantages
notes
thanks to @its_me for the .htaccess method as this gave me the exact regex expressions i needed