My permalinks is in the format domain.com/post-id/post-slug
I notice this is a great choice for permalinks as I can share articles with a short URL like domain.com/123
But when I go to this short url, should it not redirect to show the full URL, becuase in the adress bar its only showing domain.com/123
Any setting to do to update the URL to the actual, without putting any additional load or the server.
UPDATE: My requirement is not to get a shortlink. Rather to convert an URL fragment to the full URL.
For eg: If I type domain.com/123/wrong-slug
it gets corrected to domain.com/123/correct-slug
but if I type domain.com/123
it doesn’t get corrected to domain.com/123/post-slug
Why?
I think the reason why
is not redirected back to
with the
/%post_id%/%postname%/
permalinks settings, is that the matched rule is:You can get that information from the global
$wp
object.There are other useful informations in
$wp
available after thewp
action:Example (post):
domain.com/123/post-slug/
Example (post):
domain.com/123/
:Example (page):
domain.com/hello-world/
:Redirect example:
Here’s an example how to redirect from a) to b) using information from the global
$wp
object:i.e. we redirect when the page query varaible is not set and the request is numeric.
ps: I just want to mention that this is using the
wp
action, that is fired rather late, but before thetemplate_redirect
action, allowing the redirect possible.I also tried to come up with an example that is not using the
$_SERVER
variable ๐Hope this helps.
The shortlink of a WordPress post is on default
domain.com/?p=30
, not the Id after the domain. If you like a permalink to the ID as shortlink for each post, then is this possible via a plugin.The follow source is a example.
But if you will use the page in the front end with this new short url, is a rewrite important. Below a solution for php and a enhancement in the htaccess.
Add the source in a plugin and add this to the front end or add in the theme file
index.php
. The source check the valuetoken
from the url. This is only possible ,if you enhance the url via mor_rewrite modul in the .htaccess, see below.The rewrite inside the
.htaccess
This is the closest I got. It makes URLs like what you wanted work:
domain.com/123
Add this anywhere (e.g. in
functions.php
), go to Settings > Permalinks and then Save.The only issue with this approach is that the the user will stay on
domain.com/123
and won’t be redirected to the canonical version (domain.com/123/post-name
). It’s not really much of an issue since you already have acanonical
link in the<head>
of the website, so it’s not an issue for search engines. It could actually be considered an advantage since the user doesn’t have to wait for a redirect.The alternative would be just using links like
domain.com/?p=123
. They are only 3 characters longer and don’t add any load on the server รขยย they already work.In the end, why do you even need short urls? Twitter automatically shortens all URLs, I do not see any need to do it nowadays, they just impair the readability of URLs.
You can use the following
htaccess rule
for redirectingdomain.com/post-id
todomain.com/post-id/post-slug
.Put this rule on top of the WordPress’s default rules.