Skip to content

Instantly share code, notes, and snippets.

@golubev-alex
Last active June 14, 2024 14:25
Show Gist options
  • Save golubev-alex/c8fc35dedad4b3eec8a1ddd559f9243e to your computer and use it in GitHub Desktop.
Save golubev-alex/c8fc35dedad4b3eec8a1ddd559f9243e to your computer and use it in GitHub Desktop.
RewriteEngine on
# Директива включает редиректы.
ErrorDocument 404 /404.html
# 404 ведет на 404.html
############################################################################
#### Редирект на нижний регистр (если поддерживается tolower) ####
############################################################################
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond expr "tolower(%{REQUEST_URI}) =~ /(.*)/"
RewriteRule [A-Z] %1 [R=301,L]
############################################################################
#### Редирект на нижний регистр (если не поддерживается tolower) ####
############################################################################
RewriteCond %{REQUEST_URI} [A-Z]
RewriteCond %{REQUEST_URI} !^/blog/lowercase-redirect.php [NC]
RewriteRule .* /lowercase-redirect.php [L]
<?php
$request_uri = $_SERVER['REQUEST_URI'];
$lowercase_uri = strtolower($request_uri);
// Сравниваем исходный и преобразованный URI, и если они не совпадают, выполняем редирект.
if ($request_uri != $lowercase_uri) {
header("Location: " . $lowercase_uri, true, 301);
exit;
}
?>
############################################################################
#### Выбор основного зеркала (с www или без www) ####
############################################################################
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
############################################################################
#### Выбор основного зеркала (на www или с www) ####
############################################################################
RewriteCond %{HTTP_HOST} ^([^www].*)$
RewriteRule ^(.*)$ https://www.%1/$1 [L,R=301]
############################################################################
#### Перенаправляем протокол http на https ####
############################################################################
RewriteCond %{HTTPS} off
# Проверяем наличие https в URL.
RewriteCond %{HTTP:X-Forwarded-Proto} !https
# Перенаправляем протокол на http.
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
############################################################################
#### Убираем index.html/index.php, если он есть в конце URL ####
############################################################################
RewriteCond %{REQUEST_URI} ^(.*)/index\.(html|php)$
# URL cодержит index.php в конце.
RewriteCond %{REQUEST_METHOD} =GET
# Выявляем GET запрос в URL (не POST).
RewriteRule ^(.*)$ %1/ [R=301,L]
# Удалить index.html/index.php из URL.
############################################################################
#### Убираем повторяющиеся слеши (/) в URL ####
############################################################################
RewriteCond %{THE_REQUEST} //
# Проверяем, повторяется ли слеш (//) более двух раз.
RewriteCond %{QUERY_STRING} !http(s|)://
# Убедимся что это не урл в GET
RewriteRule .* /$0 [R=301,L]
# Исключаем все лишние слеши.
############################################################################
#### Убираем слеши в конце URL для статических файлов (содержит точку) ####
############################################################################
RewriteCond %{REQUEST_URI} \..+$
# Если файл содержит точку.
RewriteCond %{REQUEST_FILENAME} !-d
# И это не директория.
RewriteCond %{REQUEST_FILENAME} -f
# Является файлом.
RewriteCond %{REQUEST_URI} ^(.+)/$
# И в конце URL есть слеш.
RewriteRule ^(.+)/$ /$1 [R=301,L]
# Исключить слеш.
############################################################################
#### Добавляем слеш(/), если его нет, и это не файл. ####
############################################################################
RewriteCond %{REQUEST_URI} !(.*)/$
# Если слеша в конце нет.
RewriteCond %{REQUEST_FILENAME} !-f
# Не является файлом.
RewriteCond %{REQUEST_URI} !\..+$
# В URL нет точки (файл).
RewriteCond %{REQUEST_URI} ^(.+)$
# В URL есть хоть один символы
RewriteRule ^(.*)$ $1/ [L,R=301]
# Добавляем слеш в конце.
############################################################################
#### если это заглавные буквы от трех до девяти например ВАСЯ.html ####
############################################################################
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
# если это английские заглавные буквы от 3 до 9
RewriteRule ^([^.]+)\.html$ https://auto-cartel.ru/$1 [R=301,L]
# то удалить html останется просто /ВАСЯ, на другие случаи не распространяется.
############################################################################
#### ЧПУ ДВИЖКА стоит всегда в самом низу правил ####
############################################################################
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.html
############################################################################
#### Компрессия статического контента для гугл спид тест ####
############################################################################
<IfModule mod_deflate.c>
AddType image/svg+xml .svg
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/svg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/javascript "access plus 1 year"
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
ExpiresByType application/x-font-ttf "access plus 1 year"
ExpiresByType application/x-font-opentype "access plus 1 year"
ExpiresByType application/x-font-woff "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
</IfModule>
<IfModule mod_headers.c>
<filesmatch "\.(ico|flv|jpg|jpeg|webp|png|gif|css|swf|woff|pdf)$">
Header set Cache-Control "max-age=31536000, public"
</filesmatch>
<filesmatch "\.(html|htm)$">
Header set Cache-Control "max-age=7200, private, must-revalidate"
</filesmatch>
<filesmatch "\.(pdf)$">
Header set Cache-Control "max-age=86400, public"
</filesmatch>
<filesmatch "\.(js|otf|ttf|woff|woff2)$">
Header set Cache-Control "max-age=31536000, private"
</filesmatch>
</IfModule>
############################################################################
#### Конец общей части, далее следует собственные директивы .htaccess ####
############################################################################
// показываем вместо robots.txt содержимое robots.php (если тот доступен)
RewriteCond %{REQUEST_FILENAME} robots.txt
RewriteCond %{DOCUMENT_ROOT}/robots.php -f
RewriteRule ^(.*)$ /robots.php [L]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment