Jump to content

Redirect a hidden/unpublished page to another?


nclm
 Share

Recommended Posts

Hello 🙂 I’m new to PW, and building my first website with it and I have a small thing I wonder what would be the cleanest way to implement.

On the homepage, I display a list of services, each with a link to a page detailing it. It’s just an information design choice to show them on the homepage in this version, and in another reality they could be listed on their own Services subpage.

To keep things organised, I do have Services subpage, set as Unpublished and Hidden, which has all the services as children.

The thing I want to do, would be to have /services to redirect to / (or /#services), while keeping all the individual services keeping their pages (/services/gardening etc.) .

What would be the most relevant way to add a redirect on the hidden/unpublished Services page, pointing to the homepage? Right now it gives 404, but it should basically be 301. Ideally there would be a “Redirect to” option in Status for the page, but that doesn’t exist so I wonder what’s the way.

Thanks!

Link to comment
Share on other sites

Hello @nclm,

if your services page has no template file, you could put this in your _init.php:

if ($_SERVER['REQUEST_URI'] == '/services/') {
    session()->redirect('/', 301);
}

Source: https://processwire.com/api/ref/session/redirect/

 

If your services page has a template file, you could put this in the template file:

session()->redirect('/', 301);

But it would only work with a hidden page.

Regards, Andreas

Edited by AndZyk
Link to comment
Share on other sites

Super thanks, very helpful! I ended up creating a redirect-home.php template and put this in it:

// Redirect to homepage
$target = $pages->get("/")->url();
session()->redirect($target, 301);

Then applied it to the service page and published the page.

(the site is in a subfolder for now so I couldn’t rely on "/" alone.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
Ă—
Ă—
  • Create New...