General Question

shivian's avatar

How do I do an automatic redirect in Drupal?

Asked by shivian (10points) October 17th, 2007

Is there anyway to have the “homepage” automatically redirect to the most recent piece of content in a specific taxonomy? I tried using Views, but since the homepage is just a view instead of an actual module, certain sidebar blocks I’ve coded that rely on that specific node page being present are no longer working. I presume this would be a piece of PHP coding that I’d likely implement using the “frontpage” module…

Observing members: 0 Composing members: 0

7 Answers

sferik's avatar

Have you tried searching the Drupal Forums? There is a large community of Drupal experts there who may have already answered your question.

I don’t want to deter you from using Fluther, but there might be a better place for these questions, which require rather specialized knowledge.

sferik's avatar

That being said, the easiest way I can think of to implement what I think you’re trying to accomplish is by configuring your front page to display a single node, ordered by date with the most recent node “on top”.

You should then be able to just publish all of your nodes in a specific taxonomy to the front page and people will see the latest node without needing to be redirected.

sferik's avatar

As a more general recommendation from an experienced Drupal user, it’s better not to use contributed modules if you can solve the problem using Drupal’s core functionality. I maintain a Drupal installation for my company's website and we are very selective about the contributed modules that we install.

Contributed modules can become a burden when you are looking to update Drupal. They may also expose you to new security vulnerabilities.

shivian's avatar

Sadly this doesn’t help my problem. As said, since the homepage view isn’t an actual view of a full module, but rather a listing teaser type thing, the functionality I would like isn’t working. I need a redirect to a full node. :-(

Btw, yes, I’ve checked on Drupal and have a post there as well :-)

sferik's avatar

Here’s an example of how to do a redirect in PHP:


To find the most recent node, I think you would have to write some SQL like this (where X, Y, and Z are the terms ids that you would like to include):

SELECT n.nid FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN (X, Y, Z) ORDER BY n.created DESC LIMIT 0, 1

You may also find this article about creating a custom home page in Drupal using Views helpful.

shivian's avatar

Thanks Sferik, however, as in my original post – I can’t use Views, I’ve already tried (and have several years experience in using the Views module). Also, the Redirect IS something I could code on my own, but again, I want it to be dynamic, not hard coded / changed every day. Thanks anyways.

sferik's avatar

Right, you have to execute the query to dynamically get the most recent node and then plunk the result of the query into the redirect, like this:

$result = db_query('SELECT n.nid FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN (X, Y, Z) ORDER BY n.created DESC LIMIT 0, 1');
while ($row = db_fetch_object($result)) {
  header('Location:' . $row->nid);

Answer this question




to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
Knowledge Networking @ Fluther