rash

[solved] Urls without parents, double content?

Recommended Posts

Hi guys,

I'm a Processwire-Newbie and new to this forum. Happily I have to struggle with very few difficulties, thanks to the clear and pleasing concept and structure of PW. Currently there is only one thing that makes me brood:

I have a main category 'posts' that contains the majority of all pages. So the regular url would be 'domain/posts/post-one' etc. As I prefer the url scheme 'domain/post-one' I followed the instructions discussed in this topic.

This hook is in my 'init.php':

wire()->addHookBefore('Page::path', function($event) {
  $page = $event->object;
  if($page->template == 'post') {
    $event->replace = true;
    $event->return = "/$page->name/";
  }
});

And this is in my 'home' template:

if(strlen($input->urlSegment2)) {
    throw new Wire404Exception();
}
else if(strlen($input->urlSegment1)) {
    $input->urlSegment1;
    $name = $sanitizer->pageName($input->urlSegment1);
    $post = $pages->get("/posts/")->child("name=$name");      
    if($post->id) echo $post->render();
    else throw new Wire404Exception();
      $renderMain = false;
} else {
  // regular homepage output
  $posts = $pages->find("parent=/posts/, limit=$limit, sort=-date");
  $body = renderPosts($posts);
}

Both templates 'home' and 'post' have 'url segments' option activated.

On the first sight everything is working fine. $page-name outputs '/domain/post-one' and the page '/domain/post-one' ist getting displayed. What's frighening me is the fact, that 'domain/posts/post-one' is working as well. This means 'post-one' can be adressed with two different urls, and I’m not sure how to rate that.

On one hand, nobody will ever notice the '/domain/posts/page-one' option, as it's listed nowhere. So I could just ignore it. On the other hand, I don't know for sure if this presumption is correct. Maybe there are unknown channels where the 'wrong' urls will be spreaded, then there will be 'doubled content' which is bad, as far as  I know.

So what I'm asking for: Is there an easy way to avoid the double url-scheme option and output a 404 error, when 'domain/posts/page-one' is called? Or should I just don't care, as it doesn't matter a all? Unfortunately, I don’t fully understand every line of the second code, so I would be very grateful if someone could light it up for me a bit.

Thanks + regards
Ralf

Share this post


Link to post
Share on other sites

Rename your post.php to _post.php and instead of $post->render() use wireRenderFile("_post", ['page' => $post]).

This way processwire doesn't automatically recognise that post does have a template file and therefore throws a 404 for those pages.

  • Like 3

Share this post


Link to post
Share on other sites

Your link to the topic you followed doesn't work, and I'm not sure what's in your renderPosts() function, but assuming that LostKobrakai is right and you are using $page->render() (edit: I see the render() call now), another alternative is to place this at the top of the template you render:

if( empty($options['pageStack']) ) throw new Wire404Exception();

 

  • Like 4

Share this post


Link to post
Share on other sites

@LostKobrakai: Thanks a lot for your fast help. I tried your suggestion and am at least a bit further. When I call 'domains/posts/post-one' a 404 is thrown out. Foolishly,  '/domain/post-one' doesn't show up anymore, instead 'domain' – the homepage – is displayed. As I said: I don't really understand what I'm doing ???

Share this post


Link to post
Share on other sites

Robin's method would work in that case. Maybe you should go with that.

  • Like 1

Share this post


Link to post
Share on other sites

@LostKobrakai: No, I don’t use $page-> viewable – there must be another issue.

@Robin S & diogo: I filled in the line you suggested and now everything works the way I was searching for. (Okay, I don’t have a clue what the line actually does, but as a newbie there must be some learning potential left.)

So big thanks to all of you for your kind answers, you were enormously helpful.

Ralf

  • Like 2

Share this post


Link to post
Share on other sites

$options is an always populated array in template scope.

And if there is no pageStack defined ($options["pageStack"]), it is a direct call to this page, what you don't want allow.

Welcome to PW and the forums! :)

  • Like 1

Share this post


Link to post
Share on other sites

@horst: Thanks for the info. I will play around with $options and see what else I can do with it. Maybe I should read the docs in total before messing things up ...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Leftfield
      Hi All 🙂

      How to append canonical URL to head from certain templates?

      Thanks!!!
    • By zota
      Hi,
      After 2 or 3 cliks on my nav bar the browser starts to show
      http://julio.x10host.com/home/juliox15/public_html/logos/
      instead of
      http://julio.x10host.com/logos/
      Where or what should I start the fixing?
      Thanks
    • By Marco Angeli
      Hi there,
      I added a ssl certificate to my site and I'd like to redirect every single http url to its new https version
      So I added this code in the .htacces file, after the RewriteEngine On :
      Redirect 301 /about https://www.mysite.it/about
      Unfortunately this is now working: I get the "too many redirects" error.
      The following code works, but it's a bulk redirection to the home page, something I don't want for SEO reasons (https://moz.com/blog/save-your-website-with-redirects😞
      RewriteCond %{HTTP_HOST} mysite\.it [NC]
      RewriteCond %{SERVER_PORT} 80
      RewriteRule ^(.*)$ https://www.mysite.it/$1 [R,L]
      Any suggestions?
    • By louisstephens
      So I have a project where multiple pages are sending POST data to 1 single template page.  All was working well (well, at least with one ajax post), but now I have hit a stumbling block. I figured  the "best" way to handle the request were to use url segments and then use the following in the status page:
      if ($config->ajax && $input->urlSegment1 == 'add-bookmark') { // some code here } However, this doesnt seem to really work (as I assume the the request isnt being posted to /status/ but rather to /status/add-bookmark/). What is the best way to actually handle this?
    • By joelplambeck
      Hi Guys,
      I'm trying to do my first migration to the customers existing server (IIS 10) . I ran the site as a subdirectory on my website for test purposes (everything works fine).
      Following the tutorial of Joss, I tryed the site on a local xampp server to make sure, it also works on a root directory. So far so good, everything works.
      Now I moved the files (from the xampp) to the customers server. The root/index page is shown but for every subpage i get 404 Errors...
      Hence I followed the troubleshooting guide for not working URLs:
      On the first sight, the .htaccess file is not recognized, therefore I contacted the host support. They said, it is recognized but not all modules are supported in the processwire .htaccess file. I did the "öalskjfdoal" test in the .htaccess file and didn't get a 500 Error.... BUT the rewrite rule from the hosts support, to proof the file is read, DID work... The support claims, they do not provide debugging... so basically the .htaccess file is recognized and working, but not throwing any errors (for whatever reason).
      Working rewrite rule (from support):
      RewriteEngine On RewriteBase / RewriteRule ^test\.asp$ index.html [NC,L] RewriteRule ^test\.html$ konzept.html [NC,L] RewriteRule ^test2\.html$ team.html [NC,L] The support said, a couple modules are not supported in the htaccess file, the supported ones are listed here: http://www.helicontech.com/ape/ (I think mod_rewrite is supported)
      As I do not completely understand what exactly is happening in the htaccess file, I'm stuck. I tried all suggestions I found regarding this topic on the forum, but none of them solved the problem.
       
      .htaccess.txt