I'm using this code to create a rewrite endpoint in a custom plugin:

function setup_seo_endpoing() {
    add_rewrite_endpoint( 'item', EP_ALL );

add_action( 'init', 'setup_seo_endpoint');

This code is running / getting called, and the endpoint works with one problem:

If I visit the home page (let's say http://example.com), it is in fact displaying the correct static page per dashboard settings.

If I attempt to visit the home page with the custom endpoint set (such as http://example.com/item/ct588), WordPress displays the blog listing.

For completeness, the below code is what I'm using (inside of a function) to get the value from the endpoint.

global $wp_query;
if ( isset( $wp_query->query_vars[ 'item' ] ) ) {
    // ... do stuff
    // This does not fire

Relevant notes:

  1. I've set the Settings => Reading => Front Page setting to be a static page.
  2. I've set the Settings => Reading => Blog page setting to a different page.
  3. The rewrite rules / query_var do work properly on inner page urls: http://example.com/sample-page/item/ct0608/
  4. I've saved permalinks multiple times

Why does the blog listing get displayed instead of the static home page?

Is it possible to use custom rewrite endpoints on the home page? No article I've found actually indicates this would / could work on the home page.

  • Can you please write the entire code/function/context where you define the endpoint? Commented Sep 24, 2016 at 21:59
  • @AhmedMahdi - I've updated the question with the complete code, including the add_action call. Commented Sep 25, 2016 at 3:03

1 Answer 1


You'll need to use a combination of add_rewrite_tag and add_rewrite_rule

function setup_seo_endpoint() {
    // Ensures the $query_vars['item'] is available
    add_rewrite_tag( '%item%', '([^&]+)' );

    // Requires flushing endpoints whenever the 
    // front page is switched to a different page
    $page_on_front = get_option( 'page_on_front' );

    // Match the front page and pass item value as a query var.
    add_rewrite_rule( '^item/([^/]*)/?', 'index.php?page_id='.$page_on_front.'&item=$matches[1]', 'top' );
    // Match non-front page pages.
    add_rewrite_rule( '^(.*)/item/([^/]*)/?', 'index.php?pagename=$matches[1]&static=true&item=$matches[2]', 'top' );
add_action( 'init', 'setup_seo_endpoint', 1);

// http://wordpress.stackexchange.com/a/220484/52463
// In order to keep WordPress from forcing a redirect to the canonical
// home page, the redirect needs to be disabled.
function disable_canonical_redirect_for_front_page( $redirect ) {
    if ( is_page() && $front_page = get_option( 'page_on_front' ) ) {
        if ( is_page( $front_page ) ) {
            $redirect = false;

    return $redirect;
add_filter( 'redirect_canonical', 'disable_canonical_redirect_for_front_page' );
  • Thanks - excellent answer. A few quick clarifications: This is instead of add_rewrite_endpoint? And - will disabling the canonical redirect from the home page cause any negative effects? Lastly - is the assignment in the if condition intention? Or should it be if (... $front_page == get_option( 'page_on_front') ) {...? Commented Sep 25, 2016 at 16:42
  • That is correct... it is instead of the add_rewrite_endpoint. Endpoints would be used to add an additional output format to a url, such as json, rss, xml, etc. The assignment is also intentional. If that option is not set, it would return as false, and when checked in the next conditional would fail, therefore not setting $redirect to false.
    – bswatson
    Commented Sep 25, 2016 at 16:44
  • Noting that the output format is not the only way to use endpoints, it's just the most common use case I've seen.
    – bswatson
    Commented Sep 25, 2016 at 16:47
  • As long as you page still has a canonical reference pointing to the home page, it shouldn't have any negative consequences, but I'm not an SEO expert and wouldn't want to definitively say it won't have any effects. If you are concerned, you might want to modify the format you are using to pass the item number to query strings using ?item=ct588. You can always rewrite your permalinks to include that info when the page is generated.
    – bswatson
    Commented Sep 25, 2016 at 17:02
  • Nice. I have implemented, works great. I modified slightly the if condition, since as a coding style preference I avoid assignments inside of an if condition. Thank you very much! I'd upvote 10 times if I could! Commented Sep 25, 2016 at 17:07

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.