Jump to content

URL Segment showing 404 when i tried to open page in browser


Junaid Farooqui
 Share

Recommended Posts

Hi guys,

i was trying to implement SEO URL structure on my another processwire website. SEO team requested us to put .html on every pages. e.g http://www.mydomain.com/products.html and if click any product then it will like http://www.mydomain.com/products/product-one.html so after little bit research i find out it can be done with URLsegment option in template options. i checked on Allow URL Segment, said No to "Should page url end with slash" and said No to "Should URL segments end with a trailing slash? " but after all this setting we try to access the it is showing 404 page. i don't know why.

Any help will be highly appreciate

Thanks 
J

Link to comment
Share on other sites

That's because a dot (.html) is not considered a segment (I think?). Segments take the form of /a/b/c/, i.e. separated by forward slashes. I am not sure whether using the regex feature would allow for this. Have a look here.  A different approach here as well:

 

  • Like 1
Link to comment
Share on other sites

For the URL /products/product-one.html, as kongondo says ".html" will not be recognised a valid URL segment in the template for "product-one". But "product-one.html" would be a valid URL segment in the template for "product". So you would need to enable URL segments for every template and at the start of every template file you could have something like this:

if($input->urlSegment1) {
    $name = strtok($input->urlSegment1, '.html');
    $p = $page->children->get("name=$name");
    if($p->id) {
        echo $p->render();
        $this->halt();
    } else {
        throw new Wire404Exception();
    }
}

 

  • Like 1
Link to comment
Share on other sites

By the way, strtok can lead to some unexpected results. Try this:

$name = $sanitizer->pageName(strtok('about-us.html', '.html'));
echo $name;// outputs 'abou'

$name = $sanitizer->pageName(strtok('around-us.html', '.html'));
echo $name;// outputs 'around-us'

So, you might want to use something else (replace or regex, etc)

  • Like 3
Link to comment
Share on other sites

11 hours ago, kongondo said:

By the way, strtok can lead to some unexpected results.

Good to know - I had read on Stack Overflow that it was an efficient way to return the string of characters occurring before a substring but never tested it.

Lets try that again... :)

At the top of template files where child pages need the custom ".html" URLs:

if(empty($options['pageStack']) && $input->urlSegment1) {
    $name = $sanitizer->pageName(rtrim($input->urlSegment1, '.html'));
    $p = $pages->findOne("parent=$page, name=$name");
    if($p->id) {
        echo $p->render();
        $this->halt();
    } else {
        throw new Wire404Exception();
    }
}

In /site/ready.php:

$this->addHookAfter('Page::path', function($event) {
    $page = $event->object;
    $tpls = ['basic_page', 'some_template']; // define template names
    if(in_array($page->template, $tpls)) {
        $return = rtrim($event->return, '/'); // remove trailing slash if present
        $event->return = $return . '.html';
    }
});

Not that I think any of this is a good idea. Better to tell the SEO team that there is no reason to prefer /my-page.html over /my-page/.

  • Like 2
Link to comment
Share on other sites

 Share

×
×
  • Create New...