3

When users enter http://www.example.com I would like to redirect them to the http://www.example.com/news template.

How could I achieve this?

3
  • Do you mean proper redirecting? Or do you only want that news channel index template to be you home page?
    – carlcs
    Aug 15, 2014 at 13:38
  • I want the news channel to be my homepage. If possible redirect to the latest post in the news page. Aug 15, 2014 at 13:40
  • 1
    "I want the news channel to be my homepage" is not that what you get with the {% redirect %} tag. No visitor would be on http://www.example.com.
    – carlcs
    Aug 15, 2014 at 14:29

4 Answers 4

8

You can do this with Craft’s {% redirect %} tag. Just put this in your /index.html template:

{% redirect 'news' %}

If you want to redirect to the latest entry in your News section, as you mentioned in the comments above, just fetch the entry first:

{% set latestNews = craft.entries.section('news').order('postDate desc').first() %}
{% redirect latestNews.url %}
4

If you want one of your channel entries to be the homepage, simply include the channel entries' template to your home page index.html:

{# Set the `entry` variable manually #}
{% set entry = craft.entries.section('news').first() %}

{# Load the channel entry template #}
{% include "news/_entry" %}

With this setup, your visitors would actually be on your homepage's URL http://www.example.com instead of never really landing there.

You'd possibly even confuse some visitors, if they get redirected to http://www.example.com/news/my-not-so-very-long-news-title straight away and they might search for your proper home page?!

I'm not sure at all, but I can imagine a regularly changing redirect (each time you add a new news post you'd also have a new home URL!) could also by problematic for SEO rankings. Please correct me if I'm wrong with that!

2
  • 1
    Using this technique you would want to set a canonical url to the sections entry url so google didn't see duplicate content on your homepage AND the news entry page.
    – jnowland
    Aug 18, 2014 at 9:34
  • Right. Thanks for adding this here, @jnowland!
    – carlcs
    Aug 18, 2014 at 9:36
3

EDIT:

This answer was given when it wasn't yet clear that @shanti wants one of the channel entries to become the home page. The following is only relevant, if you want a channel's index page to be available under http://www.example.com.

.

I'd recommend you to add a single section to your configuration as the entry for your channel's index. In this single's settings you select the template you currently use and you have a checkbox to make this single entry your home page!

Another idea would be to use one structure section for all singular content pages (e.g. about, contact, etc.) and all your channel index pages (e.g. projects, news). There are other interesting Q/As here on Stack Exchange on that topic. If you're going that way, have a look at the answers on "Add home page to a structure?"

If you simply want to redirect the visitors to http://www.example.com/news, there's a {% redirect %} tag available for such redirects.

2

Another approach if you simply want to use a channel as your homepage (rather than redirecting) is to just make your homepage extend the channel's index template. For example, set your homepage template to:

{% extends "news/index" %}

...and the News channel will appear on your homepage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.