Skip to content

Instantly share code, notes, and snippets.

@aktaumag
Created March 12, 2020 08:13
Show Gist options
  • Save aktaumag/cd3b7f71fc588166725dd9850ff5b15c to your computer and use it in GitHub Desktop.
Save aktaumag/cd3b7f71fc588166725dd9850ff5b15c to your computer and use it in GitHub Desktop.
Основные перенаправления и прочие настройки в файле .htaccess
@aktaumag
Copy link
Author

aktaumag commented Mar 12, 2020

RewriteEngine On

# START WebSEO.kz Michael Nossov
 # порядок правил в файле должен быть именно такой, чтобы не возникало повторных перенаправлений
 #
 # переадресация со страниц без финального слеша на страницы с финальным слешом и сразу на HTTPS и без-WWW
RewriteCond %{REQUEST_URI} !\?
RewriteCond %{REQUEST_URI} !\&
RewriteCond %{REQUEST_URI} !\=
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !\/$
RewriteRule ^(.*)$ https://domen.ru/$1/ [L,R=301]
 #
 # переадресация с WWW версии сайта на без-WWW и сразу на HTTPS
RewriteCond %{HTTP_HOST} ^www.domen.ru$ [NC]
RewriteRule ^(.*)$ https://domen.ru/$1 [R=301,L]
 #
 # переадресация с HTTP версии сайта на HTTPS
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
 #
# END WebSEO.kz

@aktaumag
Copy link
Author

aktaumag commented Mar 12, 2020

Перенаправление на страницу без index.php

БИТРИКС

<?php
// START WebSEO.kz Michael Nossov:
	$wscanonical = 'https://' . $_SERVER['HTTP_HOST'] . str_replace('index.php', '', $APPLICATION->GetCurPage(true));
	if(isset($_REQUEST['PAGEN_1']) && !empty($_REQUEST['PAGEN_1']) && intval($_REQUEST['PAGEN_1']) > 1){
		$wscanonical .= '?PAGEN_1='.intval($_REQUEST['PAGEN_1']);
	}
    // Перенаправление на страницу без index.php
	$ws_uri_parts = explode('?', $_SERVER['REQUEST_URI'], 2);
	if(substr($ws_uri_parts[0], -10) == '/index.php'){
		header("HTTP/1.1 301 Moved Permanently");
		header("Location: ".$wscanonical);
		exit();
	}
// END WebSEO.kz
?>

ДРУГОЕ

// Перенаправление на страницу без index.php
$ws_uri_parts = explode('?', $_SERVER['REQUEST_URI'], 2);
$ws_uri_protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$ws_uri_canonical = $ws_uri_protocol . $_SERVER['HTTP_HOST'] . $ws_uri_parts[0];
if(substr($ws_uri_parts[0], -10) == '/index.php'){
	header("HTTP/1.1 301 Moved Permanently");
	header("Location: ".$ws_uri_canonical );
	exit();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment