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]


Mario Peshev is a 5x CEO and operator, founder of DevriX and Growth Shuttle, global value creation advisor, angel investor, and author of “MBA Disrupted.”

His original background in engineering rode the wave of IT entrepreneurship in the last 25 years, from product and service entrepreneurship through acquiring and selling businesses, to investing in global startups like beehiiv, doola, the Stacked Marketer, Alcatraz, SeedBlink.

Peshev spent over 10,000 hours in consulting and training contracts for mid-market and enterprise organizations like VMware, SAP, Software AG, CERN, Saudi Aramco since 2006. His books and guides are referenced in over 50 universities in North America, Europe, and Asia.


Follow Mario on social:

Latest Editions:

Latest Answers: