Alfred Allen
Alfred Allen

Reputation: 23

How to redirect Url that has %E2%80%8C code

I have a few Urls on my site that have "%E2%80%8C" in them which I want to redirect to their specific pages.

For Example: https://suntrics.com/tag/features%E2%80%8C-%E2%80%8Cof%E2%80%8C-%E2%80%8Ca-%E2%80%8Chealthy%E2%80%8C-%E2%80%8Cwork%E2%80%8C-%E2%80%8Cenvironment%E2%80%8C/

Want to redirect to: https://suntrics.com/tag/features-of-a-healthy-work-environment/

I have tried this code on my .htaccess.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^/]+)/.*?[&%] [NC] RewriteRule ^ / [L,R]

It is working but it's redirecting to my homepage and not to the page which I want. Also, this code creating problem when I create a new post in the wordpress so I had to remove it.

So could you guys please help me to fix this?

Upvotes: 1

Views: 246

Answers (1)

Krunal Bhimajiyani
Krunal Bhimajiyani

Reputation: 1179

You can do that by using the template_redirect hook.

Try out this code in your functions.php file.

add_action( 'template_redirect', 'redirect_custom_url' );

function redirect_custom_url() {
    $uri      = $_SERVER['REQUEST_URI'];
    $host     = $_SERVER['HTTP_HOST'];
    $protocol = $_SERVER['REQUEST_SCHEME'];
    $crt_url  = $protocol . '://' . $host . $uri;

    $pattern  = '(%E2%80%8C|%e2%80%8c)';

    if ( preg_match( $pattern, $crt_url ) === 1 ) {
        $redirect_url = preg_replace( $pattern, '', $crt_url );
        wp_safe_redirect( $redirect_url );
        exit;
    }
}

Upvotes: 1

Related Questions