Created
September 12, 2022 11:24
-
-
Save andronex/067185d40936eabaae754d8ab04ad3c0 to your computer and use it in GitHub Desktop.
Редиректы с маршрутизацией статического сайта
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DirectoryIndex index.php | |
RewriteEngine On | |
RewriteBase / | |
# Rewrite www.example.com -> example.com | |
RewriteCond %{HTTP_HOST} . | |
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC] | |
RewriteRule ^(.*)$ https://%1/$1 [R=301,L] | |
# Force rewrite to https for every host | |
RewriteCond %{HTTPS} !=on [OR] | |
RewriteCond %{SERVER_PORT} !^443 | |
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] | |
#404 error page | |
ErrorDocument 404 /404.html | |
# remove //// | |
RewriteCond %{REQUEST_URI} ^(.*?)\/{2,}(.*?)$ | |
RewriteRule . %1/%2 [L,R=301] | |
RewriteCond %{THE_REQUEST} // | |
RewriteRule .* /$0 [R=301,L] | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA] | |
# remove index.html | |
RewriteCond %{REQUEST_METHOD} =GET | |
RewriteCond %{REQUEST_URI} ^(.*)/index\.html$ | |
RewriteRule ^(.*)$ %1/ [R=301,L] | |
# serves a .webp image instead of jpg/png | |
RewriteCond %{HTTP_ACCEPT} image/webp | |
RewriteCond %{REQUEST_FILENAME} ^(.+)\.(jpe?g|png)$ | |
RewriteCond %1\.webp -f | |
RewriteRule ^(.+)\.(jpe?g|png)$ $1.webp [T=image/webp,E=accept:1] | |
AddType image/webp .webp | |
# For servers that support output compression, you should pick up a bit of | |
# speed by un-commenting the following lines. | |
php_flag zlib.output_compression On | |
php_value zlib.output_compression_level 7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// функция возврата данных запрошенной страницы | |
function returnRequest($uri){ | |
return file_get_contents(dirname(__FILE__) . '/' . ltrim($uri, '/')); | |
} | |
// функция редиректов | |
// для добавления нового редиректа просто добавьте аналогичную строку в вызов функции ниже, например: | |
// case 'старый/урл.html': $out_uri = 'новый-урл.html'; break; | |
function getRedirect($uri){ | |
$out_uri = ''; | |
switch($uri){ | |
case 'services/tech/main.html': $out_uri = 'vyvoz-bytovoj-tekhniki-besplatno.html'; break; | |
case 'services/furniture/main.html': $out_uri = 'vyvoz-mebeli-besplatno.html'; break; | |
case 'services/trash/batteries.html': $out_uri = 'vyvoz-chugunnyh-batarej-besplatno.html'; break; | |
case 'services/electro/main.html': $out_uri = 'vyvoz-elektroniki-besplatno.html'; break; | |
case 'services/trash/main.html': $out_uri = 'vyvoz-krupnogabaritnogo-musora-besplatno.html'; break; | |
case 'services/trash/pallets.html': $out_uri = 'vyvoz-derevyannyh-poddonov-besplatno.html'; break; | |
case 'services/trash/baths.html': $out_uri = 'vyvoz-chugunnoj-vanny-besplatno.html'; break; | |
case 'services/trash/windows.html': $out_uri = 'vyvoz-okon-besplatno.html'; break; | |
case 'services/waste/boards.html': $out_uri = 'vyvoz-dosok-besplatno.html'; break; | |
case 'services/things/extinguishers.html': $out_uri = 'vyvoz-ognetushitelej-besplatno.html'; break; | |
case 'services/things/books.html': $out_uri = 'vyvoz-knig-besplatno.html'; break; | |
case 'services/tech/microwaves.html': $out_uri = 'vyvoz-mikrovolnovok-besplatno.html'; break; | |
case 'services/things/tires.html': $out_uri = 'vyvoz-shin-besplatno.html'; break; | |
case 'services/electro/lamps.html': $out_uri = 'vyvoz-lamp-besplatno.html'; break; | |
case 'services/tech/washing_machines.html': $out_uri = 'vyvoz-stiralnyh-mashin-besplatno.html'; break; | |
case 'services/trash/piano.html': $out_uri = 'vyvoz-pianino-besplatno.html'; break; | |
case 'services/trash/doors.html': $out_uri = 'vyvoz-zheleznyh-dverej-besplatno.html'; break; | |
case 'services/things/clothes.html': $out_uri = 'vyvoz-odezhdy-besplatno.html'; break; | |
case 'services/electro/computers.html': $out_uri = 'vyvoz-kompyuterov-besplatno.html'; break; | |
case 'services/furniture/cabinets.html': $out_uri = 'vyvoz-shkafov-besplatno.html'; break; | |
case 'services/furniture/walls.html': $out_uri = 'vyvoz-stenok-i-servantov-besplatno.html'; break; | |
case 'services/furniture/beds.html': $out_uri = 'vyvoz-krovatej-besplatno.html'; break; | |
case 'services/electro/tvs.html': $out_uri = 'vyvoz-televizorov-besplatno.html'; break; | |
case 'services/tech/conditioners.html': $out_uri = 'vyvoz-kondicionerov-besplatno.html'; break; | |
case 'services/tech/dishwashers.html': $out_uri = 'vyvoz-posudomoechnyh-mashin-besplatno.html'; break; | |
case 'services/trash/metal.html': $out_uri = 'vyvoz-metalloloma-besplatno.html'; break; | |
case 'services/things/main.html': $out_uri = 'vyvoz-nenuzhnyh-veshchej-besplatno.html'; break; | |
case 'services/furniture/sofas.html': $out_uri = 'vyvoz-divanov-besplatno.html'; break; | |
case 'services/electro/monitors.html': $out_uri = 'vyvoz-monitorov-besplatno.html'; break; | |
case 'services/trash/bowls.html': $out_uri = 'vyvoz-unitazov-besplatno.html'; break; | |
case 'services/waste/paper.html': $out_uri = 'vyvoz-makulatury-besplatno.html'; break; | |
case 'services/furniture/mattresses.html': $out_uri = 'vyvoz-matrasov-besplatno.html'; break; | |
case 'services/waste/recyclables.html': $out_uri = 'vyvoz-vtorsyrya-besplatno.html'; break; | |
case 'services/waste/plastic.html': $out_uri = 'vyvoz-plastika-besplatno.html'; break; | |
case 'services/waste/main.html': $out_uri = 'vyvoz-othodov-besplatno.html'; break; | |
case 'services/tech/heaters.html': $out_uri = 'vyvoz-vodonagrevatelej-besplatno.html'; break; | |
case 'services/electro/cartridges.html': $out_uri = 'vyvoz-kartridzhej-besplatno.html'; break; | |
case 'services/waste/brick.html': $out_uri = 'vyvoz-betona-i-kirpicha-besplatno.html'; break; | |
case 'services/waste/glass.html': $out_uri = 'vyvoz-stekla-besplatno.html'; break; | |
case 'services/waste/gbi.html': $out_uri = 'vyvoz-zhbi-besplatno.html'; break; | |
case 'services/tech/sewing_machines.html': $out_uri = 'vyvoz-shvejnyh-mashin-besplatno.html'; break; | |
case 'services/main.html': $out_uri = 'uslugi-po-vyvozu-besplatno.html'; break; | |
case 'services/tech/electric_stoves.html': $out_uri = 'vyvoz-elektroplit-besplatno.html'; break; | |
case 'services/tech/gas_stoves.html': $out_uri = 'vyvoz-gazovyh-plit-besplatno.html'; break; | |
case 'services/tech/refrigerators.html': $out_uri = 'vyvoz-holodilnikov-besplatno.html'; break; | |
} | |
return file_exists(dirname(__FILE__) . '/' . ltrim($out_uri, '/'))?ltrim($out_uri, '/'):''; | |
} | |
// ниже правки вносить только обдуманно | |
$get_url = parse_url($_SERVER['REQUEST_URI']); | |
$uri = 'index.html'; | |
$args = ''; | |
if(isset($get_url['query']) && $get_url['query']){ | |
$args = '?' . $get_url['query']; | |
} | |
if($_GET['q']){ | |
$uri = (string)$_GET['q']; | |
if(file_exists(dirname(__FILE__) . '/' . ltrim($uri, '/'))){ | |
die( returnRequest($uri) ); | |
} | |
else{ | |
if( $uri = getRedirect($uri) ) { | |
http_response_code(301); | |
header("Location: {$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}/{$uri}{$args}"); | |
exit; | |
} | |
http_response_code(404); | |
$uri = '404.html'; | |
die( returnRequest($uri) ); | |
} | |
} | |
if(preg_match('/\/index/iu', $get_url['path'])){ | |
http_response_code(301); | |
header("Location: {$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}{$args}"); | |
exit; | |
} | |
die( returnRequest($uri) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment