P
P
Paltos2018-05-23 20:54:34
PHP
Paltos, 2018-05-23 20:54:34

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');
?>

Such in htacces
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]

how to make it better?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Paltos, 2018-05-26
@Paltinik

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;
}

htaccess
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.* /index.php

Norm? Or is it better to process such requests in htacces (such as removing slashes, query string, etc. ..)?

A
Alexander Tokarchuk, 2018-05-24
@Kewa2008

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]

S
stepar, 2018-05-25
@stepar

via js

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question