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:

[php]
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 );
exit;
}

return $request;
}

add_filter( ‘request’, ‘dx_redirector_request_filter’ );
[/php]


My name is Mario Peshev, a global SME Business Advisor running digital businesses for 20 the past years.

Born in Bulgaria, Europe, I gained diverse management experience through my training work across Europe, North America, and the Arab world. With 10,000+ hours in consulting and training for organizations like SAP, VMware, CERN, I’ve dedicated a huge amount of my time to helping hundreds of SMEs growing in different stages of the business lifecycle.

My martech agency DevriX grew past 50 people and ranks as a top 10 WordPress global agency and Growth Blueprint, my advisory firm, has served 400+ SME founders and executives with monthly ongoing strategy sessions.


Follow me at:

Latest Editions:

Browse by Category

Latest Answers: