> For the complete documentation index, see [llms.txt](https://docs.umbraco.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.umbraco.com/umbraco-cms/17.latest/develop-with-umbraco/application-code/backend-and-custom-logic/routing/request-pipeline/find-publishedcontent-and-template.md).

# FindPublishedContentAndTemplate()

The followed method is called on the "PublishedContentRequest.PrepareRequest()" method: `FindPublishedContentAndTemplate()`. We discuss shortly what this method is doing:

1. FindPublishedContent ()
2. Handles redirects
3. HandlePublishedContent()
4. FindTemplate()
5. FollowExternalRedirect()
6. HandleWildcardDomains()

#### HandlePublishedContent

* No content?
* Run the LastChanceFinder
* Is an IContentFinder, resolved by ContentLastChanceFinderResolver
* By default, is null (= ugly 404)
* Follow internal redirects
* Take care of infinite loops
* Ensure user has access to published content
* Else redirect to login or access denied published content
* Loop while there is no content
* Take care of infinite loops

#### FindTemplate

* Use altTemplate if
* Initial content
* Internal redirect content, and InternalRedirectPreservesTemplate is true
* No alternate template?
* Use the current template if one has already been selected
* Else use the template specified for the content, if any
* Alternate template?
  * Use the alternate template, if any
  * Else use what’s already there: a template, else none
* Alternate template is used only if displaying the intended content
  * Except for internal redirects
  * If you enable InternalRedirectPreservesTemplate
  * Which is false by default
* Alternate template replaces whatever template the finder might have set
  * ContentFinderByNiceUrlAndTemplate
  * /path/to/page/template1?altTemplate=template2  template2
* Alternate template does not falls back to the specified template for the content
  * /path/to/page?altTemplate=missing  no template
  * Even if the page has a template
* But preserves whatever template the finder might have set
  * /path/to/page/template1?altTemplate=missing  template1

#### FollowExternalRedirect()

* Content.GetPropertyValue("umbracoRedirect")
* If it’s there, sets the published content request to redirect to the content
* Will trigger an external (browser) redirect

#### HandleWildcardDomains()

![Culture and Hostnames](/files/Zo2CKebJNff829fixaPx)

* Finds the deepest wildcard domain between
* Domain root (or top)
* Request’s published content
* If found, updates the request’s culture accordingly

This implements separation between hostnames and cultures


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.umbraco.com/umbraco-cms/17.latest/develop-with-umbraco/application-code/backend-and-custom-logic/routing/request-pipeline/find-publishedcontent-and-template.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
