Home Redirection HTTP/1.1 301 Moved Permanently
Reply: 1

Redirection HTTP/1.1 301 Moved Permanently

Fernando Rossi
1#
Fernando Rossi Published in 2018-02-07 14:24:03Z

I have the following files. The objective of this is to redirect to the correct news. For example: localhost/tostadotv/esto-es-una-noticia-28.html

If I intentionally modify the url, for example: localhost/tostadotv/esto-es-una-noticia-modificada-incorrecta-28.html

I should redirect myself to the correct news: localhost/tostadotv/esto-es-una-noticia-28.html

However, it redirects me to this: http://localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/esto-es-una-noticia-28.html

Where this error? Could you please help me thanks. Excuse my english I'm from Argentina I do not speak English

.htaccess

RewriteEngine On
RewriteRule ^.*-([0-9]+)\.html$ noticia.php?id_not=$1 [L]

noticia.php

<?php require_once("lib/connection.php"); ?>
<?php require_once("lib/functions.php"); ?>
<?php
fix_category_product_url();
?>

functions.php

function fix_category_product_url() {      
    $proper_url = get_proper_category_product_url(1);

    if ( SITE_DOMAIN.$_SERVER['REQUEST_URI'] != $proper_url) { 
        header('HTTP/1.1 301 Moved Permanently'); 
        header('Location: '.$proper_url);
        exit();         
    }
}
function get_proper_category_product_url($id) {   
    $product_id = $_GET['id_not'];

    $query = sprintf('SELECT titulo FROM noticias WHERE id_not = "%d"', mysqli_real_escape_string($GLOBALS['DB'], $product_id));
    $restit = mysqli_query($GLOBALS['DB'], $query);
    $noticia = mysqli_fetch_array($restit);

    $proper_url = make_category_product_url($noticia['titulo'], $product_id, $id);

    return $proper_url;
}
define('SITE_DOMAIN', 'localhost');
function _prepare_url_text($string) {    
    $NOT_acceptable_characters_regex = '#[^-a-zA-Z0-9_ ]#';
    $string = iconv('UTF-8','ASCII//TRANSLIT',$string);
    $string = preg_replace($NOT_acceptable_characters_regex, '', $string);

    $string = trim($string); 

    $string = preg_replace('#[-_ ]+#', '-', $string); 

    return $string;
}

function make_category_product_url($product_name, $product_id, $ido) { 
    $clean_product_name = _prepare_url_text($product_name);

    if ($ido == 0)
        $url = strtolower($clean_product_name).'-'.$product_id.'.html'; 
    else 
        $url = SITE_DOMAIN.'/tostadotv/'.strtolower($clean_product_name).'-'.$product_id.'.html'; 

    return $url;
}
Zeke
2#
Zeke Reply to 2018-02-07 22:20:06Z

As said in the comments, the final solution for the asker was to add http:// to the defined SITE_DOMAIN constant.

Before

define('SITE_DOMAIN', 'localhost');

After

define('SITE_DOMAIN', 'http://localhost');

But there's more to it than just that. Let's focus on the following two functions:

function fix_category_product_url(){      
    $proper_url = get_proper_category_product_url(1);
    if(SITE_DOMAIN.$_SERVER['REQUEST_URI'] != $proper_url){ 
        header('HTTP/1.1 301 Moved Permanently'); 
        header('Location: '.$proper_url);
        exit();         
    }
}

function make_category_product_url($product_name, $product_id, $ido) { 
    $clean_product_name = _prepare_url_text($product_name);
    if($ido == 0)
        $url = strtolower($clean_product_name).'-'.$product_id.'.html'; 
    else 
        $url = SITE_DOMAIN.'/tostadotv/'.strtolower($clean_product_name).'-'.$product_id.'.html'; 
    return $url;
}

The idea here is that $proper_url actually ends up getting a value from make_category_product_url() because its result is returned by get_proper_category_product_url(). It makes sense because make_category_product_url() has more parameters and uses the other to get their values.

What's funny about this is that the else block of the second function doesn't always return a path, but rather a URL. The problem here is that such URL is given without a defined protocol, but starts with the domain name instead. This value is therefore mistaken as a path.

Now take a look at the first function: it ultimately redirects the user using header('Location: '.$proper_url);. As we discussed earlier, $proper_url is not always a path, so the protocol should be added somewhere in the code whenever a URL takes place instead of a path. That's where the actual solution comes in: adding http:// where SITE_DOMAIN is defined is one way to do this, because this constant is only used when a URL takes place. There are many other ways to do this, but this one is completely valid.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.352332 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO