Redirect all pages to homepage but still allow dashboard

I only want users to be able to use the dashboard of my WordPress site. I have a custom login form on the homepage but apart from that I don’t want them to be able to access anything else on the front end.

I have seen the following htaccess fule…

# BEGIN redirect to homepage
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js|php)$
RewriteCond %{REQUEST_URI} !^/wp-admin [NC]
RewriteRule .* / [L,R=301]
#END redirect to homepage

Which looks like it would do what I need, but is there a better way to achieve it?

1 Answer
1

Just use ‘ template_redirect ‘ hook

This action hook executes just before WordPress determines which template page to load. It is a good hook to use if you need to do a redirect with full knowledge of the content that has been queried.

add_action('template_redirect','redirect_all_pages_to_home');

function redirect_all_pages_to_home() {
    if ( ! is_front_page() ) {
        wp_redirect( get_home_url() );
        exit;
    }
}

Leave a Comment