Home > Enterprise >  How to redirect Url that has ‌ code
How to redirect Url that has ‌ code

Time:11-14

I have a few Urls on my site that have "‌" in them which I want to redirect to their specific pages.

For Example: https://suntrics.com/tag/features‌-‌of‌-‌a-‌healthy‌-‌work‌-‌environment‌/

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?

CodePudding user response:

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  = '(‌|‌)';

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