0

I have a WordPress URL that looks like this www.domain.com/wordpress-page but I want people to be able to retrieve this page even if they type it without the hyphens (i.e., www.domain.com/wordpresspage). Is there a WP setting that will redirect the incorrect URL to the correct one?

  • Could it be the contrary, each time the user enters wordpress-page redirect to wordpresspage? – Thiago Locks Feb 13 '13 at 22:14
3

I'm sure there is a more elegant way of achieving your desires, however this method does work. Just tested it.

/**
 * Redirect pages from array.
 *
 * @author Michael Ecklund
 * @author_url https://www.michaelbrentecklund.com/
 *
 * @return void
 */
function redirect_pages() {

    // Page from => Page to
    $redirect_pages = array(
        'wordpresspage' => '/wordpress-page'
    );

    // Check all required redirections.
    foreach( $redirect_pages as $page_from => $page_to ) {

        // Check the current page.
        if( is_page( $page_from ) ) {

            // Redirect to proper page.
            wp_redirect( site_url( $page_to ), 301 );
            exit;

        }

    }
}

In order to fire the redirection properly, you will need to add a page for "wordpresspage", so that it can properly detect the current page and redirect to domain.com/wordpress-page/.

Add the action in order to trigger the hook.

add_action( 'template_redirect', 'redirect_pages' );
  • @James Please accept my answer if you feel satisfactory with my response. – Michael Ecklund Feb 13 '13 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.