Add htaccess for clean URLs
Also rename main files to index.php to avoid the need for custom directory indices. Attached is a script for testing.
This commit is contained in:
29
.htaccess
29
.htaccess
@@ -1,3 +1,26 @@
|
|||||||
ErrorDocument 404 /main.php?page=404
|
ErrorDocument 404 /index.php?page=404
|
||||||
DirectoryIndex mainkrk.php main.php index.php start.html index.html
|
|
||||||
Options -Indexes
|
# 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]
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
$lang = empty($_GET['lang']) ? 'cs' : $_GET['lang'];
|
$lang = empty($_GET['lang']) ? 'cs' : $_GET['lang'];
|
||||||
$page = $_GET['page'] ?? '';
|
$page = $_GET['page'] ?? '';
|
||||||
|
|
||||||
|
ob_start();
|
||||||
?>
|
?>
|
||||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||||
<html>
|
<html>
|
||||||
@@ -140,10 +142,10 @@ echo '<span class="floatright">';
|
|||||||
if (file_exists("pages/$page-de.php") || file_exists("pages/$page/$page-de.php")) {
|
if (file_exists("pages/$page-de.php") || file_exists("pages/$page/$page-de.php")) {
|
||||||
if (file_exists("pages/$page/$page-de.php")) {
|
if (file_exists("pages/$page/$page-de.php")) {
|
||||||
$lngc = 'de';
|
$lngc = 'de';
|
||||||
$icona = "<a href=\"mainkrk.php?page=$page/$page&lang=$lngc\"><img src=\"/images/" . $lngc . 'f.png" alt=""></a>';
|
$icona = "<a href=\"/krk/$lngc/$page/$page.html\"><img src=\"/images/" . $lngc . 'f.png" alt=""></a>';
|
||||||
} else {
|
} else {
|
||||||
$lngc = 'de';
|
$lngc = 'de';
|
||||||
$icona = '<a href="mainkrk.php?page=' . htmlspecialchars($page) . "&lang=$lngc\"><img src=\"/images/" . $lngc . 'f.png" alt=""></a>';
|
$icona = "<a href=\"/krk/$lngc/" . htmlspecialchars($page) . '.html"><img src="/images/' . $lngc . 'f.png" alt=""></a>';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($lang == 'de') {
|
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) . '-en.php') || file_exists('pages/' . htmlspecialchars($page) . '/' . htmlspecialchars($page) . '-en.php')) {
|
||||||
if (file_exists('pages/' . htmlspecialchars($page) . '/' . htmlspecialchars($page) . '-en.php')) {
|
if (file_exists('pages/' . htmlspecialchars($page) . '/' . htmlspecialchars($page) . '-en.php')) {
|
||||||
$lngc = 'en';
|
$lngc = 'en';
|
||||||
$icona = '<a href="mainkrk.php?page=' . htmlspecialchars($page) . '/' . htmlspecialchars($page) . "&lang=$lngc\"><img src=\"/images/" . $lngc . 'f.png" alt=""></a>';
|
$icona = "<a href=\"/krk/$lngc/" . htmlspecialchars($page) . '/' . htmlspecialchars($page) . '.html"><img src="/images/' . $lngc . 'f.png" alt=""></a>';
|
||||||
} else {
|
} else {
|
||||||
$lngc = 'en';
|
$lngc = 'en';
|
||||||
$icona = '<a href="mainkrk.php?page=' . htmlspecialchars($page) . "&lang=$lngc\"><img src=\"/images/" . $lngc . 'f.png" alt=""></a>';
|
$icona = "<a href=\"/krk/$lngc/" . htmlspecialchars($page) . '.html"><img src="/images/' . $lngc . 'f.png" alt=""></a>';
|
||||||
}
|
}
|
||||||
if ($lang == 'en') {
|
if ($lang == 'en') {
|
||||||
echo '';
|
echo '';
|
||||||
@@ -167,7 +169,7 @@ if (file_exists('pages/' . htmlspecialchars($page) . '-en.php') || file_exists('
|
|||||||
}
|
}
|
||||||
} elseif ($page === '' && file_exists('pages/krk_default-en.php')) {
|
} elseif ($page === '' && file_exists('pages/krk_default-en.php')) {
|
||||||
$lngc = 'en';
|
$lngc = 'en';
|
||||||
$icona = "<a href=\"mainkrk.php?page=krk_default&lang=$lngc\"><img src=\"/images/" . $lngc . 'f.png" alt=""></a>';
|
$icona = "<a href=\"/krk/$lngc/\"><img src=\"/images/" . $lngc . 'f.png" alt=""></a>';
|
||||||
if ($lang == 'en') {
|
if ($lang == 'en') {
|
||||||
echo '';
|
echo '';
|
||||||
} else {
|
} 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) . '.php') || file_exists('pages/' . htmlspecialchars($page) . '/' . htmlspecialchars($page) . '.php')) {
|
||||||
if (file_exists('pages/' . htmlspecialchars($page) . '/' . htmlspecialchars($page) . '.php')) {
|
if (file_exists('pages/' . htmlspecialchars($page) . '/' . htmlspecialchars($page) . '.php')) {
|
||||||
$lngc = '';
|
$lngc = '';
|
||||||
$icona = '<a href="mainkrk.php?page=' . htmlspecialchars($page) . '/' . htmlspecialchars($page) . $lngc . '"><img src="/images/' . $lngc . 'f.png" alt=""></a>';
|
$icona = '<a href="/krk/' . htmlspecialchars($page) . '/' . htmlspecialchars($page) . '.html"><img src="/images/' . $lngc . 'f.png" alt=""></a>';
|
||||||
} else {
|
} else {
|
||||||
$lngc = '';
|
$lngc = '';
|
||||||
$icona = '<a href="mainkrk.php?page=' . htmlspecialchars($page) . $lngc . '"><img src="/images/' . $lngc . 'f.png" alt=""></a>';
|
$icona = '<a href="/krk/' . htmlspecialchars($page) . '.html"><img src="/images/' . $lngc . 'f.png" alt=""></a>';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($lang == 'cs') {
|
if ($lang == 'cs') {
|
||||||
@@ -189,9 +191,9 @@ if (file_exists('pages/' . htmlspecialchars($page) . '.php') || file_exists('pag
|
|||||||
echo $icona;
|
echo $icona;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($lang == 'en' && empty($page)) {
|
if ($lang == 'en' && ($page === '' || $page === 'krk_default')) {
|
||||||
$lngc = '';
|
$lngc = '';
|
||||||
$icona = '<a href="mainkrk.php?page=krk_default"><img src="/images/' . $lngc . 'f.png" alt=""></a>';
|
$icona = '<a href="/krk/"><img src="/images/' . $lngc . 'f.png" alt=""></a>';
|
||||||
echo $icona;
|
echo $icona;
|
||||||
}
|
}
|
||||||
echo '</span>';
|
echo '</span>';
|
||||||
78
test.fish
Normal file
78
test.fish
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user