Jump to content

Redirect to a page if the name exists (don't throw a 404)


cb2004
 Share

Recommended Posts

I have a site that has its landing pages underneath the URL /lp/ to keep things tidy in the admin. I have had to change things along the way with different ProcessWire versions and a bug has just been discovered with Facebook links as they add a variable to the URL. What is the best approach for the latest version? I am pretty sure you can make the names of certain templates unique so that would be my first step, and then I am thinking of plugging into ProcessPageView::pageNotFound

Cheers all.

Link to comment
Share on other sites

Hi,

what have you already tried? Not really understand your actual issue. So you have a landing page under path /lp/ and then I am lost? What updates have caused which problem? What do you want to achieve, what code are you using now, whats your PW version used …

Link to comment
Share on other sites

Hi @cwsoft, sorry you are right, is was a very vague Sunday post whilst trying to spend time with the family. So basically there could be /lp/offer1/ but if I went to /offer1/ I would like it to redirect to the correct page, as this is a shorter URL for printed material. This is what we have currently in _init.php

<?php namespace ProcessWire;

$wire->addHookBefore('ProcessPageView::pageNotFound', function($e) {
    $name = str_replace('/', '', $_SERVER['REQUEST_URI']);
    $count = wire('pages')->count("name=$name, include=hidden");
    if($count) {
        $id = wire('pages')->find("name=$name, include=hidden");
        $url = wire('pages')->get("$id")->url;
        wire('session')->redirect($url);
    }
});

Cheers

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