1

I want to have two different URLs for the same page. In particular I want to have a specific URL that shows my frontpage. The URL should be kept however. So these two URLs

www.mywebsite.com/
www.mywebsite.com/page1

should both be displaying the frontpage. I thought this could be done with WordPress's add_rewrite_rule function, but I didn't have success with that yet.

When I do this

add_rewrite_rule('^page1', 'index.php', 'top');

It acts more like a redirect. When I try to access www.mywebsite.com/page1 I get redirected to my homepage. But I want the URL to stay the same, which this doesn't seem to do.

When I do this

add_rewrite_rule('^page1', 'index.php?pagename=$matches[1]', 'top');

it just shows the page www.mywebsite.com/page1 instead of the homepage.

I fumbled around with the rewrite rules for hours now and I'm pretty much at my wits end. Is what I want even possible?


Edit: This is different from many other questions in that I am trying to show the frontpage. The solution provided here solves this, when you can use the locations query var, but I didn't get it to work with that.

6
  • 4
    Possible duplicate of How do I add a add_rewrite_rule without it redirecting? Commented Apr 15, 2018 at 16:01
  • However, you shouldn't do this. It's very bad for SEO, it would be normally considered duplicate content. It would be better for you to add an additional landing page to your site. Commented Apr 15, 2018 at 16:03
  • Thank you. Unfortunately I don't make that decision. Had these concerns as well, but it has to be done... Commented Apr 15, 2018 at 19:07
  • Unfortunately that doesn't solve my problem. I think I can't use 'locations" in my query, because I'm pointing to my homepage. Commented Apr 16, 2018 at 10:36
  • Under Settings > Reading, what is Your homepage displays set to?
    – Milo
    Commented Apr 16, 2018 at 16:49

1 Answer 1

3

WordPress will always try to redirect to the canonical URL to prevent duplicate content, so you'll have to disable that mechanism to get another URL to display that page.

First, your rewrite rule:

add_rewrite_rule('^page1', 'index.php?pagename=your-page-name', 'top');

Then filter redirect_canonical to return false if the requested URL is your 2nd home URL:

function my_redir_check( $redirect_url, $requested_url ){
    if( home_url( '/page1/' ) == trailingslashit( $requested_url ) ){
        return false;
    }
    return $redirect_url;
}
add_filter( 'redirect_canonical', 'my_redir_check', 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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