<p>This works in Craft 3</p>

<pre><code>{% set url = craft.app.request.url %}

{% if url == "/" %}
   homepage
{% endif %}
</code></pre>

CC BY-SA 4.0
2020-07-30T11:09:22.017Z
It doesn't work if the user comes to the page via something like Facebook, which would add a `/?fbclid=XXXXX` parameter to the end of the URL
Rob Hodges
CC BY-SA 4.0
2020-07-30T11:17:14.553Z
I've gone with:

`{% if not craft.app.request.segments|length %}`
Rob Hodges
CC BY-SA 4.0
2019-11-13T16:53:35.973Z
2019-11-13T16:53:35.973Z
triplethreat23
<p>Our <code>homepage</code> is set via <a href="https://craftcms.com/docs/3.x/entries.html#entry-uri-formats" rel="nofollow noreferrer">Craft's recommendation</a>:</p>
<blockquote>
<p>You can designate any one entry as a site’s homepage using a special <strong>home</strong> URI.</p>
</blockquote>
<p>Since the <code>homepage</code> is an entry, it will always have an <code>entry</code> object with a <a href="https://docs.craftcms.com/api/v3/craft-base-elementtrait.html#uri" rel="nofollow noreferrer">uri</a>, so I can use a simple one-liner like:</p>
<p><code>{% set isHomepage = entry is defined and entry.uri == &quot;__home__&quot; ? true : false %}</code></p>

CC BY-SA 4.0
2020-11-29T18:46:55.880Z
2021-12-09T17:10:00.503Z
2021-12-09T17:10:00.503Z
Tammy Shipps
<p>I'd rather:</p>

<pre><code>{% if entry.uri == "__home__" %}
  
{% endif %}
</code></pre>

CC BY-SA 4.0
2020-09-07T18:25:31.533Z
this only works if the page is pulling from an entry, otherwise: Variable "entry" does not exist.
Leo Leoncio
CC BY-SA 4.0
2020-11-29T18:36:53.350Z
@LeoLeoncio The homepage is an entry, so if you are checking if whatever you're on is the homepage or not, you can assume false if there's no entry object. Otherwise, it's a custom setup and answering would require more info.
Tammy Shipps
CC BY-SA 4.0
2020-04-11T18:12:13.250Z
2020-04-11T18:12:13.250Z
Urs
<p>The best option for Craft 3 and 4 is probably <a href="https://docs.craftcms.com/api/v3/craft-base-element.html#ishomepage" rel="nofollow noreferrer"><code>entry.isHomepage</code></a>, i.e.</p>
<pre><code>{% if entry is defined and entry.isHomepage %}
   This is the homepage
{% endif %}
</code></pre>

CC BY-SA 4.0
2021-12-22T23:06:59.497Z
2023-07-25T12:44:39.957Z
2023-07-25T12:44:39.957Z
Mats Mikkel Rummelhoff
<p>This works nicely and limits error possiblities:</p>
<pre><code>{% if craft.app.urlManager.matchedElement and craft.app.urlManager.matchedElement.uri == '__home__' %}
    Show on homepage
{% endif %}
</code></pre>

CC BY-SA 4.0
2022-01-10T01:03:55.503Z
2022-01-10T01:03:55.503Z
Dario Zadro
<p>To prevent a Twig exception, you may wish to use <code>entry is defined</code> to catch this error.</p>

<p><a href="https://twig.symfony.com/doc/2.x/tests/defined.html" rel="nofollow noreferrer">https://twig.symfony.com/doc/2.x/tests/defined.html</a></p>

<p>Another way to detect the homepage would be looking at the request with <code>craft.app.request</code>, something like <code>craft.app.request.pathInfo</code>, if it equals <code>/</code>, then you've got your condition.</p>

CC BY-SA 4.0
2019-03-30T14:58:58.143Z
2019-03-30T15:28:02.647Z
2019-03-30T15:28:02.647Z
James White
<p>Another option for you. Check for the presence of a url segment.</p>
<pre><code>{% if not craft.app.request.segment(1) %}
    This is the homepage
{% endif %}
</code></pre>

CC BY-SA 4.0
2021-10-20T15:55:07.087Z
2021-10-20T15:55:07.087Z
foamcow
<p>My current approach</p>
<pre><code>{% if entry.isHomepage|default %}
Homepage
{% endif %}
</code></pre>
<p>Tested in Craft4</p>

CC BY-SA 4.0
2023-07-21T04:45:29.777Z
2023-07-21T04:45:29.777Z
Leevi Graham
<p>My site is localized, so I ended up doing...</p>
<pre><code>{% if craft.app.request.url == '/' or craft.app.request.url == '/' ~ craft.app.language %}
</code></pre>

CC BY-SA 4.0
2020-09-07T18:30:19.117Z
2020-09-07T18:30:19.117Z
Leo Leoncio
<p>I'm trying to run a check within my <code>_layout.html' to remove the navigation bar if the user is on the homepage. I've tried with the following code below, but browsing to a page which is the</code>index.html` of a Channel breaks the code with the following error:</p>

<blockquote>
  <p>Variable "entry" does not exist.</p>
</blockquote>

<p>This is the code within my <code>_layout.html</code></p>

<pre><code>{% set handle = entry.section.handle %}
{% if handle == 'homepage' %}
{% else %}
  {% if not craft.app.user.isGuest %}
      {% set users = craft.users %}
      {% include '_navigation'%}
    {% else %}
  {% endif %}
{% endif %}
</code></pre>

<templating>
Check if current page is the homepage
# Check if current page is the homepage

(Asked by: username_0 on 2019-03-30)

I'm trying to run a check within my _layout.html' to remove the navigation bar if the user is on the homepage. I've tried with the following code below, but browsing to a page which is theindex.html` of a Channel breaks the code with the following error:

Variable "entry" does not exist.

This is the code within my _layout.html
{% set handle = entry.section.handle %}
{% if handle == 'homepage' %}
{% else %}
  {% if not craft.app.user.isGuest %}
      {% set users = craft.users %}
      {% include '_navigation'%}
    {% else %}
  {% endif %}
{% endif %}

**Tags:** templating

---

## 9 answers

### Answer by username_1 on 2019-11-13. Score: 3 | Accepted answer

This works in Craft 3
{% set url = craft.app.request.url %}

{% if url == "/" %}
   homepage
{% endif %}

> **Comments:**
> **username_2**: It doesn't work if the user comes to the page via something like Facebook, which would add a `/?fbclid=XXXXX` parameter to the end of the URL  
> **username_2**: I've gone with:  `{% if not craft.app.request.segments|length %}`  

---

### Answer by username_3 on 2020-11-29. Score: 9

Our `homepage` is set via [Craft's recommendation](https://craftcms.com/docs/3.x/entries.html#entry-uri-formats):

> You can designate any one entry as a site’s homepage using a special **home** URI.

Since the `homepage` is an entry, it will always have an `entry` object with a [uri](https://docs.craftcms.com/api/v3/craft-base-elementtrait.html#uri), so I can use a simple one-liner like:

`{% set isHomepage = entry is defined and entry.uri == "__home__" ? true : false %}`


---

### Answer by username_4 on 2020-04-11. Score: 7

I'd rather:
{% if entry.uri == "__home__" %}
  
{% endif %}

> **Comments:**
> **username_5**: this only works if the page is pulling from an entry, otherwise: Variable "entry" does not exist.  
> **username_3**: @LeoLeoncio The homepage is an entry, so if you are checking if whatever you're on is the homepage or not, you can assume false if there's no entry object. Otherwise, it's a custom setup and answering would require more info.  

---

### Answer by username_6 on 2021-12-22. Score: 7

The best option for Craft 3 and 4 is probably entry.isHomepage, i.e.
{% if entry is defined and entry.isHomepage %}
   This is the homepage
{% endif %}


---

### Answer by username_7 on 2022-01-10. Score: 2

This works nicely and limits error possiblities:
{% if craft.app.urlManager.matchedElement and craft.app.urlManager.matchedElement.uri == '__home__' %}
    Show on homepage
{% endif %}


---

### Answer by username_8 on 2019-03-30. Score: 2

To prevent a Twig exception, you may wish to use `entry is defined` to catch this error.

<https://twig.symfony.com/doc/2.x/tests/defined.html>

Another way to detect the homepage would be looking at the request with `craft.app.request`, something like `craft.app.request.pathInfo`, if it equals `/`, then you've got your condition.


---

### Answer by username_9 on 2021-10-20. Score: 2

Another option for you. Check for the presence of a url segment.
{% if not craft.app.request.segment(1) %}
    This is the homepage
{% endif %}


---

### Answer by username_10 on 2023-07-21. Score: 1

My current approach
{% if entry.isHomepage|default %}
Homepage
{% endif %}

Tested in Craft4


---

### Answer by username_5 on 2020-09-07. Score: 1

My site is localized, so I ended up doing...
{% if craft.app.request.url == '/' or craft.app.request.url == '/' ~ craft.app.language %}


---

