diff --git a/.htaccess b/.htaccess index f4dd0a9..ecfd54a 100644 --- a/.htaccess +++ b/.htaccess @@ -1,3 +1,26 @@ -ErrorDocument 404 /main.php?page=404 -DirectoryIndex mainkrk.php main.php index.php start.html index.html -Options -Indexes \ No newline at end of file +ErrorDocument 404 /index.php?page=404 + +# Redirect old URLs to new ones +RewriteCond &%{QUERY_STRING} &lang=(cs|en|de) [NC] +RewriteCond %1@&%{QUERY_STRING} (cs|en|de)@.*&page=([^&]+) [NC] +RewriteRule ^((index|mainkrk|print)\.php)?(\?.*)?$ /krk/%1/%2.html [R=301,QSD,L,nosubreq] + +RewriteCond &%{QUERY_STRING} &page=([^&]+) [NC] +RewriteRule ^((index|mainkrk|print)\.php)?(\?.*)?$ /krk/%1.html [R=301,QSD,L,nosubreq] + +RewriteCond &%{QUERY_STRING} &lang=(cs|en|de) [NC] +RewriteRule ^((index|mainkrk|print)\.php)?(\?.*)?$ /krk/%1/ [R=301,QSD,L,nosubreq] + +# Canonization +RewriteRule ^cs(?:/(.*))?$ /krk/$1 [R=301,L] +RewriteRule ^(en|de)$ /krk/$1/ [R=301,L] +RewriteRule ^(mainkrk|index|print).php$ /krk/ [R=301,L] + +### Fall back to loading pages through our “CMS” +RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-f +RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-d +RewriteRule ^(en|de)/$ /krk/index.php?lang=$1 [END] +RewriteRule ^(en|de)/(.+)\.html$ /krk/index.php?page=$2&lang=$1 [END] +RewriteRule ^(.+)\.html$ /krk/index.php?page=$1 [END] +RewriteRule ^(en|de)/(.+)/$ /krk/index.php?page=$2&lang=$1 [END] +RewriteRule ^(.+)/$ /krk/index.php?page=$1 [END] diff --git a/mainkrk.php b/index.php similarity index 88% rename from mainkrk.php rename to index.php index 8e00841..e596d09 100644 --- a/mainkrk.php +++ b/index.php @@ -1,6 +1,8 @@ @@ -140,10 +142,10 @@ echo ''; if (file_exists("pages/$page-de.php") || file_exists("pages/$page/$page-de.php")) { if (file_exists("pages/$page/$page-de.php")) { $lngc = 'de'; - $icona = "'; + $icona = "'; } else { $lngc = 'de'; - $icona = ''; + $icona = "'; } if ($lang == 'de') { @@ -155,10 +157,10 @@ if (file_exists("pages/$page-de.php") || file_exists("pages/$page/$page-de.php") if (file_exists('pages/' . htmlspecialchars($page) . '-en.php') || file_exists('pages/' . htmlspecialchars($page) . '/' . htmlspecialchars($page) . '-en.php')) { if (file_exists('pages/' . htmlspecialchars($page) . '/' . htmlspecialchars($page) . '-en.php')) { $lngc = 'en'; - $icona = ''; + $icona = "'; } else { $lngc = 'en'; - $icona = ''; + $icona = "'; } if ($lang == 'en') { echo ''; @@ -167,7 +169,7 @@ if (file_exists('pages/' . htmlspecialchars($page) . '-en.php') || file_exists(' } } elseif ($page === '' && file_exists('pages/krk_default-en.php')) { $lngc = 'en'; - $icona = "'; + $icona = "'; if ($lang == 'en') { echo ''; } else { @@ -177,10 +179,10 @@ if (file_exists('pages/' . htmlspecialchars($page) . '-en.php') || file_exists(' if (file_exists('pages/' . htmlspecialchars($page) . '.php') || file_exists('pages/' . htmlspecialchars($page) . '/' . htmlspecialchars($page) . '.php')) { if (file_exists('pages/' . htmlspecialchars($page) . '/' . htmlspecialchars($page) . '.php')) { $lngc = ''; - $icona = ''; + $icona = ''; } else { $lngc = ''; - $icona = ''; + $icona = ''; } if ($lang == 'cs') { @@ -189,9 +191,9 @@ if (file_exists('pages/' . htmlspecialchars($page) . '.php') || file_exists('pag echo $icona; } } -if ($lang == 'en' && empty($page)) { +if ($lang == 'en' && ($page === '' || $page === 'krk_default')) { $lngc = ''; - $icona = ''; + $icona = ''; echo $icona; } echo ''; diff --git a/test.fish b/test.fish new file mode 100644 index 0000000..638659c --- /dev/null +++ b/test.fish @@ -0,0 +1,78 @@ +set -g domain 'http://new.tojnar.cz' +set -g failed 0 + +function fail + set -l message $argv[1] + set -l output_file $argv[2] + echo "$message" > /dev/stderr + cat "$output_file" > /dev/stderr + set failed (math $failed + 1) +end + +function test_request + set -l uri $argv[1] + set -l argc (count $argv) + + echo "Testing $domain$uri" > /dev/stderr + set output_file (mktemp) + curl --silent --location --head "$domain$uri" > "$output_file" + set last_status (cat "$output_file" | grep --perl-regexp 'HTTP/.+ \K([0-9]{3})' --only-matching | tail -n 1 | string trim) + set last_location (cat "$output_file" | grep --perl-regexp 'Location: \K(.+)' --only-matching | tail -n 1 | string trim) + + set -l expected_status 200 + if test "$argc" -ge 2 + set expected_status $argv[2] + end + if test "$last_status" != "$expected_status" + fail "ERROR: Expected status $expected_status, $last_status received" "$output_file" + return + end + + if test "$argc" -ge 3 + set -l expected_location $argv[3] + if test -z "$last_location" + fail "ERROR: Expected location $expected_location but no received" "$output_file" + return + else if test "$last_location" != "$domain$expected_location" + fail "ERROR: Expected location $expected_location, $last_location received" "$output_file" + return + end + else if test -n "$last_location" + fail "ERROR: Unexpected location $last_location received" "$output_file" + return + end + + echo "OK" > /dev/stderr + echo > /dev/stderr +end + +test_request '/krk?lang=cs&page=krk_default' 200 '/krk/' +test_request '/krk?lang=en&page=krk_default' 200 '/krk/en/' +test_request '/krk?lang=cs' 200 '/krk/' +test_request '/krk?lang=en' 200 '/krk/en/' +test_request '/krk/mainkrk.php?page=clanky/beskydy-2010' 200 '/krk/clanky/beskydy-2010.html' +test_request '/krk/mainkrk.php?page=clanky/beskydy' 404 '/krk/clanky/beskydy.html' +test_request '/krk/?page=mcr_2009' 200 '/krk/mcr_2009/' +test_request '/krk/?page=mcr_2009/mcr_2009' 200 '/krk/mcr_2009/' +test_request '/krk/?page=mcr_2009/probihani_loucna/probihani_loucna' 200 '/krk/mcr_2009/probihani_loucna/' +test_request '/krk/?page=clanky' 200 '/krk/clanky/' +test_request '/krk/?page=clanky/clanky' 200 '/krk/clanky/' +test_request '/krk/?page=mcr_2009/' 200 '/krk/mcr_2009/' +test_request '/krk/?page=foo/' 404 '/krk/foo/' +test_request '/krk' 200 '/krk/' +test_request '/krk/cs/' 200 '/krk/' +test_request '/krk/en/' +test_request '/krk/en' 200 '/krk/en/' +test_request '/krk/cs' 200 '/krk/' +test_request '/krk/mainkrk.php' 200 '/krk/' +test_request '/krk/cs/mcr_2009/mcr_2009.html' 200 '/krk/mcr_2009/mcr_2009.html' +test_request '/krk/en/mcr_2009/mcr_2009.html' +test_request '/krk/clanky/beskydy-2010.html' +test_request '/krk/clanky/beskydy.html' 404 + +if test $failed = 0 + echo "All tests passed" > /dev/stderr +else + echo "$failed tests failed" > /dev/stderr + exit 1 +end