[英]Redirection HTTP/1.1 301 Moved Permanently
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
例如: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 如果我有意修改了网址,例如: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 我应该将自己重定向到正确的消息: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 但是,它将我重定向到以下位置: 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 .htaccess
RewriteEngine On
RewriteRule ^.*-([0-9]+)\.html$ noticia.php?id_not=$1 [L]
noticia.php noticia.php
<?php require_once("lib/connection.php"); ?>
<?php require_once("lib/functions.php"); ?>
<?php
fix_category_product_url();
?>
functions.php 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;
}
As said in the comments, the final solution for the asker was to add http://
to the defined SITE_DOMAIN
constant. 如评论中所述,对
SITE_DOMAIN
者的最终解决方案是将http://
添加到已定义的SITE_DOMAIN
常量。
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()
. 这里的想法是
$proper_url
实际上最终从make_category_product_url()
获得一个值,因为它的结果是由get_proper_category_product_url()
返回的。 It makes sense because make_category_product_url()
has more parameters and uses the other to get their values. 这是有道理的,因为
make_category_product_url()
具有更多参数,并使用其他参数来获取它们的值。
What's funny about this is that the else
block of the second function doesn't always return a path, but rather a URL. 有趣的是,第二个函数的
else
块并不总是返回路径,而是返回URL。 The problem here is that such URL is given without a defined protocol, but starts with the domain name instead. 这里的问题是,给出的URL没有定义的协议,而是以域名开头。 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);
现在看一下第一个函数:它最终使用
header('Location: '.$proper_url);
重定向用户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. 正如我们前面所讨论的,
$proper_url
并不总是path ,因此,只要出现URL而不是路径 ,都应该将协议添加到代码中的某个位置。 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. 这就是实际解决方案的
SITE_DOMAIN
http://
在其中定义了SITE_DOMAIN
地方添加http://
是实现此目的的一种方法,因为此常量仅在发生URL时使用。 There are many other ways to do this , but this one is completely valid. 还有许多其他方法可以执行此操作 ,但是这种方法是完全有效的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.