WordPress redirection based on URL

We’ve been working here on a URL shortener as a part of another project. To avoid the server redirect rules and so, we verify against the database whether a slug exists (is already entered in the DB) and if so, we get the URL and redirect to the page.

As a fan of the request filter, I’d go with this approach:

function dx_redirector_request_filter( $request ) {
// check for request[‘name’], for instance for
// somewpsite.com/some-slug $request[‘name’]
// would be some-slug
if( ! empty( $request[‘name’] ) ) {
// verify against the database entries
// redirect accordingly, this is a sample
// if you pick somewpsite.com/yahoo.com or so
wp_redirect( ‘http://’ . $request[‘name’], 301 );

return $request;

add_filter( ‘request’, ‘dx_redirector_request_filter’ );

Leave a Reply

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