Peter Knight Posted May 24, 2023 Share Posted May 24, 2023 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 More sharing options...
BitPoet Posted May 24, 2023 Share Posted May 24, 2023 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); 1 Link to comment Share on other sites More sharing options...
Peter Knight Posted May 24, 2023 Author Share Posted May 24, 2023 Thanks BitPoet. I had that setup but wasn't sure it was using a 301 redirect method for SEO. I also have a nice page created via ListerPro which lists all the redirects I have made. Handy for an at-a-glance view etc Link to comment Share on other sites More sharing options...
wbmnfktr Posted May 27, 2023 Share Posted May 27, 2023 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. 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 More sharing options...
Peter Knight Posted June 7, 2023 Author Share Posted June 7, 2023 Hi @wbmnfktr PagePathHistory only works when a pagesURL changes to something new. In this instance, I was looking to redirect to a completely new page. I never noticed the options under the What other URLs redirect... section but they might be useful. Thanks 1 Link to comment Share on other sites More sharing options...
wbmnfktr Posted June 9, 2023 Share Posted June 9, 2023 On 6/7/2023 at 4:40 PM, Peter Knight said: I was looking to redirect to a completely new page. Ok, I missed this detail somehow. These additional options are awesome for internal linking - at least in a solid setup. Link to comment Share on other sites More sharing options...
hassanizhar Posted June 13, 2023 Share Posted June 13, 2023 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. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now