Block page from view

Is there a way to hide a page from the public? I don’t mean toggling its visibility by adding or removing the number in front of the content folder, but actually hiding a page so that it becomes inaccessible when directly called.

In this case I’m setting up a very simple blog, where each article is a subpage. It has been decided that these articles should be shown in full on the “blog” page, and that there shouldn’t be a dedicated page for each article.

My guess is that I’ll have to tweak the .htaccess file, but I’m sure there’s a cleaner way to do this…

Thanks for any tip!

You could use a route, so that if the subpage is directly accessed, it will bump you to the same post on the main blog page. If you give each post a UID in the HTML ID, you should be able to jump to it directly. See the docs on routes here.

I don’t think .htaccess will work because you cant jump to an ID within a page via an .htaccess redirect.

This is completely untested, and its based on code from one of my sites where i do the reverse, but i think you want something like this…

array(
'pattern' => 'blog/(:any)',
'action'  => function($uid) {

  $page = page('blog'. '#' . $uid);

  if(!$page) $page = page('blog'. '#' . $uid);
  if(!$page) $page = site()->errorPage();

  return site()->visit($page);

}
),

that should redirect:

domain.com/blog/my-blog-post

to:

domain.com/blog#my-blog-post

That will not work, I’m afraid. There are several issues:

  1. $page = page('blog'. '#' . $uid);
    This will never be true

  2. if(!$page) $page = page('blog'. '#' . $uid);
    This line repeats the above with the same issue

3. return site()->visit($page);
This line does not redirect but return the page under the same URL

Suggested solution

c::set('routes', [
  [
    'pattern' => 'blog/(:any)',
    'action'  => function($uid) {
    
         $page = page('blog');

         if(!$page) $page = site()->errorPage();

         return go($page->url().'#'.$uid, 301);
     }
  ]
]);

If you don’t want to redirect anywhere, but rather just redirect to the error page:

c::set('routes', [
  [
    'pattern' => 'blog/(:any)',
    'action'  => function($uid) {
    
         return go('error');
     }
  ]
]);
1 Like