Cameron
Cameron

Reputation: 661

WordPress URL Modification

I have a WordPress page at

http://www.mysiteurl.com/pagename/

and I want any URL of the form

http://www.mysiteurl.com/pagename/{any string here}

to redirect or display my original page: http://www.mysiteurl.com/pagename/. How do I go about doing this?

Upvotes: 1

Views: 51

Answers (1)

Yuan Lung Luo
Yuan Lung Luo

Reputation: 68

Like what Kodos Johnson suggested, the function should be something like this:

function redirect_to_page() {
  //get parsed uri separated by '/'
  $a_uri = explode('/', $_SERVER['REQUEST_URI']);   
  //check if the page is the page you want to redirect   
  if( $a_uri[1] === 'pagename' ) {
    //redirect to siteurl/pagename    
    wp_redirect( home_url('pagename'));
    exit();
  }
}

add_action('template_redirect', 'redirect_to_page');

Hope this helps!

Upvotes: 2

Related Questions