Jump to content

301 redirects by PageID - missing functionality?


Peter Knight
 Share

Recommended Posts

I've been looking at various Redirect Modules and I don't think my current requirement is possible with any of them.

I want to enter a PageID (my source page) and create a 301 redirect to a different URL (destination).

Normally you might do this in the htaccess file with page paths but I'd rather do something within PW itself.

Jumplinks comes close to supporting this but doesn't support page IDs.

The reason I need page IDs to be specified in the source and destination is that my client often updates the page path for SEO experiments etc.

If I were to use page Paths as the source and destination, they might stop working.

Link to comment
Share on other sites

It's pretty easy to implement that without a module.

Create a page reference field and add it to all applicable templates. Let's call it 'redirect_page'.

In your prepend template file, add a little snippet of code that checks if redirect_page is set and issues the redirect if it is.

<?php

if($page->redirect_page)
  $session->redirect($page->redirect_page->url);

 

  • Like 1
Link to comment
Share on other sites

On 5/24/2023 at 11:41 AM, Peter Knight said:

The reason I need page IDs to be specified in the source and destination is that my client often updates the page path for SEO experiments etc.

If I were to use page Paths as the source and destination, they might stop working.

What about the core module Page Path History? It takes care of page URLs in case they change and redirect from old to new URLs.

ProcessWire is quite good at managing old URLs, at least for those created in your ProcessWire instance. In case of a migration it's something totally different. There is even a managing area under the settings tab in each page.

2023-05-27_21-00.thumb.png.64ba45f868541449c38b0717dfb4de82.png

There would be no real need to add redirects anymore. Unless you want to use something that never existed before.

Link to comment
Share on other sites

  • 2 weeks later...

It seems like you're looking for a way to create 301 redirects within ProcessWire (PW) using page IDs instead of page paths. While there may not be a specific module that supports this exact requirement, you can achieve the desired functionality by implementing a custom solution within ProcessWire.

Here's a general approach you can follow:

Create a new field in your Page template to store the destination URL. Let's call this field "Redirect URL" for reference.

In the template file for the source page, retrieve the "Redirect URL" field value for that specific page. You can use the $page API variable to access the current page object and retrieve the field value.

In the same template file, issue a 301 redirect using the retrieved "Redirect URL" value. You can use the wire()->session->redirect() method to perform the redirect.

Here's a sample code snippet to illustrate the process:

php
Copy code
// Retrieve the Redirect URL field value for the current page
$redirectURL = $page->redirect_url;

// Perform the 301 redirect
wire()->session->redirect($redirectURL, 301);
Remember to adjust the field name and code to match your actual field name and template structure.

By storing the destination URL in a custom field within ProcessWire, you can update it independently of the page path, allowing for flexibility in SEO experiments or changes.

  • 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...