def best_match_for_path(self, path, raise404=False):
        """
        Return the best match for a path. If the path as given is unavailable,
        continues to search by chopping path components off the end.

        Tries hard to avoid unnecessary database lookups by generating all
        possible matching URL prefixes and choosing the longest match.

        Page.best_match_for_path('/photos/album/2008/09') might return the
        page with url '/photos/album/'.
        """

        paths = ["/"]
        path = path.strip("/")

        if path:
            tokens = path.split("/")
            paths += ["/%s/" % "/".join(tokens[:i]) for i in range(1, len(tokens) + 1)]

        try:
            page = (
                self.active()
                .filter(_cached_url__in=paths)
                .extra(select={"_url_length": "LENGTH(_cached_url)"})
                .order_by("-_url_length")[0]
            )

            if not page.are_ancestors_active():
                raise IndexError("Parents are inactive.")

            return page

        except IndexError:
            if raise404:
                raise Http404()

        raise self.model.DoesNotExist

        Return the best match for a path. If the path as given is unavailable,
        continues to search by chopping path components off the end.

        Tries hard to avoid unnecessary database lookups by generating all
        possible matching URL prefixes and choosing the longest match.

        Page.best_match_for_path('/photos/album/2008/09') might return the
        page with url '/photos/album/'.
        
feincms/module/page/models.py