Oñay Sheard
Oñay Sheard

Reputation: 80

How can I redirect a URL containing a #?

I have a link that needs to be redirected elsewhere. My CMS (WP Engine) typically handles redirects, however, I am stuck with the following.

(placeholder domain for client privacy)

LINK A: www.test.com/#/contacts

LINK B: www.test.com/another-page

How can I successfully redirect A to B?

Is there any special steps (CMS related or not) that I need to do to make a URL with a hash symbol redirect?

Upvotes: 0

Views: 773

Answers (1)

Xhynk
Xhynk

Reputation: 13850

So you only want to forward urls with #? If it's just one page, you can just string match the current url with the page you want to redirect.

add_action( 'template_redirect', function(){
    $current_page = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

    if( strpos( $current_page, '/#/contact' ) !== false ){
        wp_redirect( site_url( '/another-page/' ) );
        exit;
    }
});

If you have multiple pages, and the permalinks are /#/slug, you can use a redirection plugin such as Content Mask, Simple 301 Redirects, or Page Links To.

Upvotes: 2

Related Questions