0

Is it possible to have specific url just for home page?

I have set that recent posts are displayed on home page and I don't want it blank but instead something like www.myweb.com/home/ and on other pages/posts I don't want that home included.

So www.myweb.com/other-page/ should not be www.myweb.com/home/other-page/

Is it possible to do?

Btw I wouldn't do that if it is not needed, this is just part of bigger issue but one solution is having that specific url for home page.

2
  • So what would be on, in your example, www.myweb.com/? May 18, 2018 at 13:17
  • nothing, that is not important in my case. Question is just example, that wp is in subfolder and it is not main web on domain May 18, 2018 at 13:20

1 Answer 1

0

First of all: this is not at all how websites behave. The standard is that you have some kind of homepage on the root URL /.

Now WordPress is customizable and I was able to achieve what you want - at least in most ways.

// used to generate the link
add_filter('page_link', 'WPSE_1805_page_link', 10, 3);
function WPSE_1805_page_link($link, $post_id, $sample) {
    // if link for any page other than home was requested, return early
    if ($link !== home_url('/'))
        return $link;

    // otherwise, return page link with slug
    return _get_page_link($post_id);
}

// don't redirect /slug to / for homepage
add_filter('redirect_canonical', 'WPSE_1805_redirect_canonical', 10, 2);
function WPSE_1805_redirect_canonical($redirect_url, $requested_url) {
    $home = get_page_link(get_option('page_on_front'));

    // if home was requested, return requested URL
    if ($requested_url === $home)
        return $requested_url;

    return $redirect_url;
}

Most necessary functions are within wp-includes/link-template.php where I found these hooks.

page_link is necessary, so /foo doesn't get rewritten to / just because it is the front page (when creating/getting page links, e.g. via get_permalink()).

redirect_canonical you need, so when a visitor comes to /foo, they do not get redirected to /.


With this, visitors can still go to / and will see the content from the selected static front page (although the canonical link will be set to /foo, so search engines should list that link instead).

To disable this, or better said redirect visitors, you can use the following

add_action('template_redirect', 'WPSE_1805_template_redirect');
function WPSE_1805_template_redirect() {
    global $wp;

    // get requested URL
    $current_url = home_url( $wp->request );
    // if requested URL is root or root with ending slash
    if ($current_url === home_url('') || $current_url === home_url('/')) {
        wp_redirect( get_page_link(get_option('page_on_front')), 301 );
        exit;
    }
}

Even with this, you will still have links going to /, simply because theme authors may use home_url('/') in their code. You could filter that, but my other code would need to be rewritten, because it too uses this function.

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.