Skip to content

Commit

Permalink
create automatically 301 redirects on page URL update
Browse files Browse the repository at this point in the history
  • Loading branch information
norbertlaposa committed Nov 28, 2017
1 parent 115bc30 commit b20fcfb
Showing 1 changed file with 47 additions and 5 deletions.
52 changes: 47 additions & 5 deletions models/common/common_uri_mapping.php
Expand Up @@ -404,15 +404,33 @@ function updateSingle($node_data) {
$item = $this->detail($list[0]['id']);

$old_uri = $item['public_uri'];
$item['public_uri'] = $this->generateSingleURIFullPath($node_data);
$new_uri = $this->generateSingleURIFullPath($node_data);

if ($old_uri != $item['public_uri']) {
if ($old_uri != $new_uri) {

/**
* delete previously created redirects for this new URI path
*/

$sql = "DELETE FROM common_uri_mapping WHERE public_uri = '".$new_uri."' AND type = '301'";
if (!is_array($this->executeSql($sql))) msg("Couldn't delete old redirects", 'error');

/**
* update selected URL
*/

$item['public_uri'] = $new_uri;

if ($this->update($item)) {

$sql = "UPDATE common_uri_mapping SET public_uri = regexp_replace(public_uri, '{$old_uri}/', '{$item['public_uri']}/') WHERE id != {$item['id']};";
$this->executeSql($sql);

// update the path also in all sub-pages
$sql = "UPDATE common_uri_mapping SET public_uri = regexp_replace(public_uri, '{$old_uri}/', '{$new_uri}/') WHERE id != {$item['id']} AND type = 'generic';";
if (!is_array($this->executeSql($sql))) msg("Couldn't update sub-pages URLs", 'error');

// insert 301 redirect
if (!$this->insertRedirect($old_uri, $node_data['id'])) msg('Redirect generator failed', 'error');

// the update was successful, altough some errors could happen
return true;

} else {
Expand Down Expand Up @@ -443,7 +461,31 @@ function insertNewPath($node_data) {
if ($id = $this->insert($item)) return $id;
else return false;
}

/**
* insert redirect
*
* @param string path
* @param int target_node_id
* @param string type, optional, 301 default
* @return int|bool
*/

public function insertRedirect($path, $target_node_id, $type = '301') {

if (trim($path) == '') return false;
if (!is_numeric($target_node_id)) return false;

$item = array();
$item['node_id'] = $target_node_id;
$item['public_uri'] = $path;
$item['type'] = $type;

if ($id = $this->insert($item)) return $id;
else return false;

}

/**
* clean title
*/
Expand Down

0 comments on commit b20fcfb

Please sign in to comment.