Answer the question
In order to leave comments, you need to log in
What's the best way to redirect?
On the site, the pages are available at site.ru/page
and they are also available at site.ru/?page and site.ru/?q=page
I want to redirect as in the first option so that there are no duplicates ..
or even better Give 404 because the pages have not been indexed (the site is still under development) The
following code is used in index.php:
<?php
$req=mb_strtolower($_SERVER['REQUEST_URI']);
if($_SERVER['REQUEST_URI']!=$req){
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$req);
die();
}
include('core/init.php');
$params = array();
$query_string = str_replace('q=', '', trim($_SERVER['QUERY_STRING']));
$query_string = urldecode($query_string);
$query_params = explode("/",trim($query_string, '/'));
foreach ($query_params as $query_param) {
$params[] = htmlspecialchars(addslashes($query_param));
};
if ($params[0] == 'home' && count($params) == "1"){
header("HTTP/1.1 302 Moved Permanently");
header("Location: /");
die();
}
if (!$params[0]) $params[0] = "home";
$file = join("/", $params);
$GLOBALS['page'] = $file;
$file = 'pages/' . $file . '.php';
if (file_exists($file)) {
include ($file);
} else if ($params[0] == "feedback" && count($params) == "1") {
include ('core/tpl/feedback.php');
} else {
include ('core/tpl/404.php');
$GLOBALS['page'] = '404';
}
include('core/tpl/template.php');
?>
AddDefaultCharset utf-8
RewriteEngine on
RewriteBase /
RewriteRule ^(.+)/$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} //
RewriteRule .* /$0 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?q=$1 [L]
Answer the question
In order to leave comments, you need to log in
Made in php
$req = parse_url($_SERVER['REQUEST_URI']);
$req = $req['path'];
if ($req != '/') {
$req = strtolower($req);
$req = preg_replace('#//+#','/',$req);
if ($req != '/') {
$req = rtrim($req, '/\\');
}
}
if ($_SERVER['REQUEST_URI'] != $req) {
header('Location: '.$req,true,301);
exit;
}
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.* /index.php
In your case, redirecting through the header is not the best idea (IMHO).
I understand correctly that you are using some kind of framework? If so, which one? Most likely, you can configure routes in the config.
If you work without a framework, then dig from the .htacces side. You
should get something like this:
RewriteCond %{QUERY_STRING} action=page [NC]
RewriteCond %{QUERY_STRING} id=(\d+) [NC]
RewriteRule .* /page/%1/? [R=301,L]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question