From: Edward Z. Yang Date: Wed, 6 May 2009 02:45:37 +0000 (-0400) Subject: Prefer extensionless URLs. X-Git-Url: https://repo.or.cz/w/xhtml-compiler.git/commitdiff_plain/76b7120b360adb407828ebca659047b6029e6e19 Prefer extensionless URLs. Signed-off-by: Edward Z. Yang --- diff --git a/htaccess.php b/htaccess.php index 4392084..a0e3a2b 100644 --- a/htaccess.php +++ b/htaccess.php @@ -79,6 +79,8 @@ if (file_exists('.htaccess')) { // build the new htaccess $n = array(); $n[] = $identifier_begin; +$n[] = 'DirectoryIndex index.html'; +$n[] = 'Options -Multiviews'; $n[] = 'RewriteEngine on'; $n[] = 'RewriteBase ' . $xc->getConf('web_path') . '/'; @@ -108,7 +110,15 @@ foreach ($allowed_dirs as $dir => $recursive) { $len = strlen($dir); $slash = (!$len || $dir[$len-1] === '/') ? '' : '/'; $dir_exp = preg_quote($dir) . $slash . $r; + $big_exp[$dir] = $dir_exp; +} +$full_dir_exp = implode('|', $big_exp); + +// prefer the extension-less URL +$n[] = 'RewriteCond %{REQUEST_URI} \.html$'; +$n[] = "RewriteRule ^(($full_dir_exp)[^/]+)\.html$ \$1 [NS,R=301]"; +foreach ($allowed_dirs as $dir => $recursive) { if (is_array($indexed_dirs)) { $intercept = isset($indexed_dirs[$dir]) ? $indexed_dirs[$dir] : true; } else { @@ -116,22 +126,16 @@ foreach ($allowed_dirs as $dir => $recursive) { } if (is_string($directory_index) && $intercept) { // setup index rewrite - $n[] = "RewriteRule ^($dir_exp)$ \$1$directory_index"; + $n[] = "RewriteRule ^({$big_exp[$dir]})$ \$1$directory_index"; } - $big_exp[] = $dir_exp; } -$full_dir_exp = implode('|', $big_exp); - // allow pretty extension-less URLs for HTML pages // this could be generalized for other URLs $n[] = 'RewriteCond %{REQUEST_FILENAME} !-f'; $n[] = 'RewriteCond %{HTTP_ACCEPT} text/html'; $n[] = "RewriteRule ^(($full_dir_exp)[^/.]+)$ \$1.html [N]"; -// prefer the extension-less URL -$n[] = "RewriteRule ^(($full_dir_exp)[^/]+)\.html$ \$1 [NS,R=301]"; - // basic redirection if it doesn't exist $n[] = 'RewriteCond %{REQUEST_FILENAME} !-f [OR]'; $n[] = 'RewriteCond %{QUERY_STRING} purge=1 [OR]';