4 * Generates the appropriate mod_rewrite rules in the htaccess file.
9 $identifier_begin = '# BEGIN xhtml-compiler/main.php mod_rewrite';
10 $identifier_end = '# END xhtml-compiler/main.php mod_rewrite';
11 $identifier_here = '# HERE xhtml-compiler/main.php mod_rewrite';
13 if (file_exists('.htaccess')) {
16 $mtime_htaccess = filemtime('.htaccess');
17 $mtime_config = filemtime('xhtml-compiler/config.php');
18 $mtime_dconfig = filemtime('xhtml-compiler/config.default.php');
19 $mtime_gen = filemtime('xhtml-compiler/htaccess.php');
20 if ($mtime_htaccess > $mtime_config &&
21 $mtime_htaccess > $mtime_gen &&
22 $mtime_htaccess > $mtime_dconfig
24 display_error_and_quit(503, false,
25 'No changes detected in <tt>xhtml-compiler/config.php</tt> or <tt>xhtml-compiler/htaccess.php</tt>.');
28 $contents = file_get_contents('.htaccess');
30 // do writeability check
33 strpos($contents, $identifier_begin) === false ||
34 strpos($contents, $identifier_end) === false
36 strpos($contents, $identifier_here) === false
38 display_error_and_quit(503, false,
39 'Pre-existing htaccess not configured to accept new rules');
42 // replace old rules with new set
45 preg_quote($identifier_begin, '/') .
47 preg_quote($identifier_end, '/') .
50 $contents = preg_replace($regex, $identifier_here, $contents);
53 $contents = $identifier_here;
56 // build the new htaccess
57 $new_contents = array();
58 $new_contents[] = $identifier_begin;
59 $new_contents[] = 'RewriteEngine on';
62 foreach ($allowed_dirs as $dir => $recursive) {
65 $r = "([$filename_chars]+\/)*";
68 $new_contents[] = "RewriteRule ^($dir_exp)$ \$1$directory_index";
69 $big_exp[] = $dir_exp;
72 $full_dir_exp = implode('|', $big_exp);
73 $new_contents[] = 'RewriteCond %{REQUEST_FILENAME} !-f [OR]';
74 $new_contents[] = 'RewriteCond %{QUERY_STRING} purge=1';
75 $new_contents[] = "RewriteRule ^(($full_dir_exp)[$filename_chars]+\.html)$ xhtml-compiler/main.php?f=\$1 [L,QSA]";
77 $new_contents[] = $identifier_end;
79 $contents = str_replace($identifier_here, implode($new_contents, PHP_EOL
), $contents);
81 file_put_contents('.htaccess', $contents);
83 ?
><h1
>200: Okay
</h1
>New <tt
>.htaccess
</tt
> file successfully written