Skip to content

Instantly share code, notes, and snippets.

@wokamoto
Last active December 16, 2015 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wokamoto/5458241 to your computer and use it in GitHub Desktop.
Save wokamoto/5458241 to your computer and use it in GitHub Desktop.
[PHP] ファイルの大文字小文字を吸収
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /canonical.php [L]
</IfModule>
<?php
function not_found($message = '') {
header("HTTP/1.1 404 Not Found");
echo $message."\n";
die();
}
if (!isset($_SERVER['REQUEST_URI']))
not_found();
$uri = htmlspecialchars($_SERVER['REQUEST_URI']);
if ($uri === '/canonical.php')
not_found();
$dir = dirname(__FILE__).dirname($uri).'/';
if (!file_exists($dir))
not_found("{$uri} is not found.");
$file = pathinfo(basename($uri));
$name = isset($file['filename']) ? $file['filename'] : '';
$ext = isset($file['extension']) ? '.'.$file['extension'] : '';
$filenames = array(
$name . strtolower($ext),
strtolower($name) . $ext,
strtolower($name) . strtolower($ext),
strtolower($name) . strtoupper($ext),
$name . strtoupper($ext),
strtoupper($name) . $ext,
strtoupper($name) . strtoupper($ext),
strtoupper($name) . strtolower($ext),
);
foreach ($filenames as $filename) {
if (file_exists($dir.$filename)) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.dirname($uri).'/'.$filename);
exit();
}
}
not_found("{$uri} is not found.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment