From e3c15ac38733c23c6d43f2a85a5800461481df5c Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Mon, 27 Apr 2009 19:17:07 -0400 Subject: [PATCH] Setup pretty URLs and prefer them. Signed-off-by: Edward Z. Yang --- htaccess.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/htaccess.php b/htaccess.php index 199a8d7..4392084 100644 --- a/htaccess.php +++ b/htaccess.php @@ -20,13 +20,13 @@ if (file_exists('.htaccess')) { // do time check $files_to_check = array( - '.htaccess.in', XHTMLCOMPILER . '/config.php', XHTMLCOMPILER . '/config.default.php', XHTMLCOMPILER . '/config.smoketest.php', XHTMLCOMPILER . '/conf/config.php', XHTMLCOMPILER . '/htaccess.php', 'redirects.txt', + '.htaccess.in', ); $mtime_htaccess = filemtime('.htaccess'); @@ -122,6 +122,17 @@ foreach ($allowed_dirs as $dir => $recursive) { } $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]'; $n[] = 'RewriteCond %{HTTP_COOKIE} purgatory=1'; @@ -167,4 +178,9 @@ $contents = str_replace($identifier_here, implode($n, PHP_EOL), $contents); file_put_contents('.htaccess', $contents); chmod('.htaccess', 0644); +if (is_cli()) { + echo "Okay: New .htaccess file successfully written\n"; + exit; +} + ?>

200: Okay

New .htaccess file successfully written -- 2.11.4.GIT