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.