Jump to content
Junaid Farooqui

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

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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

What @Robin S said.

Good idea to sanitise that name though ;). I'm sure Robin just forgot:).

 $name = $sanitizer->pageName(strtok($input->urlSegment1, '.html'));

 

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By neophron
      Hi guys,
      after getting a complain message from google about a robots.txt (where everything is ok), I searched for an online tool, where I can test my robots.txt files. I found this website: https://technicalseo.com/tools/
      This page offers a bunch of nice tools, just wanted to share it with you.

    • By Mike Rockett
      Docs & Download: rockettpw/seo/markup-sitemap
      Modules Directory: MarkupSitemap
      Composer: rockett/sitemap
      MarkupSitemap is essentially an upgrade to MarkupSitemapXML by Pete. It adds multi-language support using the built-in LanguageSupportPageNames. Where multi-language pages are available, they are added to the sitemap by means of an alternate link in that page's <url>. Support for listing images in the sitemap on a page-by-page basis and using a sitemap stylesheet are also added.
      Example when using the built-in multi-language profile:
      <url> <loc>http://domain.local/about/</loc> <lastmod>2017-08-27T16:16:32+02:00</lastmod> <xhtml:link rel="alternate" hreflang="en" href="http://domain.local/en/about/"/> <xhtml:link rel="alternate" hreflang="de" href="http://domain.local/de/uber/"/> <xhtml:link rel="alternate" hreflang="fi" href="http://domain.local/fi/tietoja/"/> </url> It also uses a locally maintained fork of a sitemap package by Matthew Davies that assists in automating the process.
      The doesn't use the same sitemap_ignore field available in MarkupSitemapXML. Rather, it renders sitemap options fields in a Page's Settings tab. One of the fields is for excluding a Page from the sitemap, and another is for excluding its children. You can assign which templates get these config fields in the module's configuration (much like you would with MarkupSEO).
      Note that the two exclusion options are mutually exclusive at this point as there may be cases where you don't want to show a parent page, but only its children. Whilst unorthodox, I'm leaving the flexibility there. (The home page cannot be excluded from the sitemap, so the applicable exclusion fields won't be available there.)
      As of December 2017, you can also exclude templates from sitemap access altogether, whilst retaining their settings if previously configured.
      Sitemap also allows you to include images for each page at the template level, and you can disable image output at the page level.
      The module allows you to set the priority on a per-page basis (it's optional and will not be included if not set).
      Lastly, a stylesheet option has also been added. You can use the default one (enabled by default), or set your own.
      Note that if the module is uninstalled, any saved data on a per-page basis is removed. The same thing happens for a specific page when it is deleted after having been trashed.
          
    • By NehaPillai
      Hello Everyone, I was trying to update SEO meta title, description and meta keywords for my website in Process Wire CMS but it saving in the backend but it is not reflecting on my website, Please help me regarding this error. Please find below attached screen shot for your ref. TIA.


    • By franciccio-ITALIANO
      Hi, we can choose the "headline" and "title" and "summery" in panel page of processwire, but we can't write the "metadecriptions" and "tags".
       I can write mdescropt and tags in templates, but I've same templates for many articles... so, how I can change mdescription and tags?

      Thanks...
    • By pwFoo
      How to enable urlSegments runtime instead of persistent in template settings? And where to be loaded before the current page returns a 404 error?
      https://processwire.com/docs/front-end/how-to-use-url-segments/#how-do-you-enable-url-segments
       
×
×
  • Create New...