View source
<?php
function htaccess_help($path, $arg) {
$output = '';
switch ($path) {
case "admin/help#htaccess":
$output = '<p>' . t("Auto generate htaccess for Drupal core based on your settings. ") . '</p>';
break;
}
return $output;
}
function htaccess_permission() {
return array(
'administer htaccess module' => array(
'title' => t('Administer htaccess module'),
'description' => t('Ability to auto generate htaccess.'),
),
);
}
function htaccess_menu() {
$items = array();
$items['admin/config/system/htaccess'] = array(
'title' => t('htaccess Settings'),
'description' => t('Set the htaccess settings here'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'htaccess_admin',
),
'access arguments' => array(
'administer htaccess module',
),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/system/htaccess/main'] = array(
'title' => t('Main'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
);
return $items;
}
function htaccess_admin() {
$options_www_prefix = array(
HTACCESS_PREFIX_WWW => t('Redirect users to www prefix'),
HTACCESS_PREFIX_NON_WWW => t('Redirect users to non www prefix'),
);
$options_symlinks = array(
HTACCESS_FOLLOWSYMLINKS => t('Options +FollowSymLinks'),
HTACCESS_SYMLINKSIFOWNERSMATCH => t('Options +SymLinksifOwnerMatch'),
);
$form['main'] = array(
'#type' => 'fieldset',
'#title' => t('Main Settings'),
'#description' => t('When clicking on Save a new root htaccess will be created based on your selections. Test this out in a development environment as an incorrect htaccess will break your entire site!'),
'#weight' => -50,
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['main']['htaccess_www_or_non_www'] = array(
'#type' => 'radios',
'#title' => t('www or non www prefix'),
'#options' => $options_www_prefix,
'#default_value' => variable_get('htaccess_www_or_non_www', HTACCESS_PREFIX_NON_WWW),
);
$form['main']['htaccess_symlinks'] = array(
'#type' => 'radios',
'#title' => t('follow symlinks or SymLinksifOwnerMatch '),
'#options' => $options_symlinks,
'#default_value' => variable_get('htaccess_symlinks', HTACCESS_FOLLOWSYMLINKS),
);
$form['#submit'][] = 'htaccess_admin_settings_submit';
return system_settings_form($form);
}
function htaccess_admin_settings_submit() {
$root_path = realpath(drupal_get_path('module', 'node') . '/../../');
$htaccess_path = $root_path . '/.htaccess';
$htaccess_lines = "#\n# Apache/PHP/Drupal settings:\n#\n\n# Protect files and directories from prying eyes.\n<FilesMatch \"\\.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\\.php)?|xtmpl)\$|^(\\..*|Entries.*|Repository|Root|Tag|Template)\$\">\n Order allow,deny\n</FilesMatch>\n\n# Don't show directory listings for URLs which map to a directory.\nOptions -Indexes\n\n# Follow symbolic links in this directory.\n";
if (variable_get('htaccess_symlinks', HTACCESS_FOLLOWSYMLINKS) == "HTACCESS_FOLLOWSYMLINKS") {
$htaccess_lines .= " Options +FollowSymLinks";
}
else {
$htaccess_lines .= " Options +SymLinksifOwnerMatch";
}
$htaccess_lines .= "\n# Make Drupal handle any 404 errors.\nErrorDocument 404 /index.php\n\n# Set the default handler.\nDirectoryIndex index.php index.html index.htm\n\n# Override PHP settings that cannot be changed at runtime. See\n# sites/default/default.settings.php and drupal_environment_initialize() in\n# includes/bootstrap.inc for settings that can be changed at runtime.\n\n# PHP 5, Apache 1 and 2.\n<IfModule mod_php5.c>\n php_flag magic_quotes_gpc off\n php_flag magic_quotes_sybase off\n php_flag register_globals off\n php_flag session.auto_start off\n php_value mbstring.http_input pass\n php_value mbstring.http_output pass\n php_flag mbstring.encoding_translation off\n</IfModule>\n\n# Requires mod_expires to be enabled.\n<IfModule mod_expires.c>\n # Enable expirations.\n ExpiresActive On\n\n # Cache all files for 2 weeks after access (A).\n ExpiresDefault A1209600\n\n <FilesMatch \\.php\$>\n # Do not allow PHP scripts to be cached unless they explicitly send cache\n # headers themselves. Otherwise all scripts would have to overwrite the\n # headers set by mod_expires if they want another caching behavior. This may\n # fail if an error occurs early in the bootstrap process, and it may cause\n # problems if a non-Drupal PHP file is installed in a subdirectory.\n ExpiresActive Off\n </FilesMatch>\n</IfModule>\n\n# Various rewrite rules.\n<IfModule mod_rewrite.c>\n RewriteEngine on\n\n # Block access to \"hidden\" directories whose names begin with a period. This\n # includes directories used by version control systems such as Subversion or\n # Git to store control files. Files whose names begin with a period, as well\n # as the control files used by CVS, are protected by the FilesMatch directive\n # above.\n #\n # NOTE: This only works when mod_rewrite is loaded. Without mod_rewrite, it is\n # not possible to block access to entire directories from .htaccess, because\n # <DirectoryMatch> is not allowed here.\n #\n # If you do not have mod_rewrite installed, you should remove these\n # directories from your webroot or otherwise protect them from being\n # downloaded.\n RewriteRule \"(^|/)\\.\" - [F]\n\n # If your site can be accessed both with and without the 'www.' prefix, you\n # can use one of the following settings to redirect users to your preferred\n # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:\n #\n # To redirect all users to access the site WITH the 'www.' prefix,\n # (http://example.com/... will be redirected to http://www.example.com/...)\n # uncomment the following:\n ";
if (variable_get('htaccess_www_or_non_www', HTACCESS_PREFIX_NON_WWW) == "HTACCESS_PREFIX_WWW") {
$htaccess_lines .= " \n RewriteCond %{HTTP_HOST} !^www\\. [NC]\n RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]\n #\n # To redirect all users to access the site WITHOUT the 'www.' prefix,\n # (http://www.example.com/... will be redirected to http://example.com/...)\n # uncomment the following:\n # RewriteCond %{HTTP_HOST} ^www\\.(.+)\$ [NC]\n # RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]\n ";
}
else {
$htaccess_lines .= " \n\t # RewriteCond %{HTTP_HOST} !^www\\. [NC]\n\t # RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]\n\t #\n\t # To redirect all users to access the site WITHOUT the 'www.' prefix,\n\t # (http://www.example.com/... will be redirected to http://example.com/...)\n\t # uncomment the following:\n\t RewriteCond %{HTTP_HOST} ^www\\.(.+)\$ [NC]\n\t RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]";
}
$htaccess_lines .= "\n # Modify the RewriteBase if you are using Drupal in a subdirectory or in a\n # VirtualDocumentRoot and the rewrite rules are not working properly.\n # For example if your site is at http://example.com/drupal uncomment and\n # modify the following line:\n # RewriteBase /drupal\n #\n # If your site is running in a VirtualDocumentRoot at http://example.com/,\n # uncomment the following line:\n # RewriteBase /\n\n # Pass all requests not referring directly to files in the filesystem to\n # index.php. Clean URLs are handled in drupal_environment_initialize().\n RewriteCond %{REQUEST_FILENAME} !-f\n RewriteCond %{REQUEST_FILENAME} !-d\n RewriteCond %{REQUEST_URI} !=/favicon.ico\n RewriteRule ^ index.php [L]\n\n # Rules to correctly serve gzip compressed CSS and JS files.\n # Requires both mod_rewrite and mod_headers to be enabled.\n <IfModule mod_headers.c>\n # Serve gzip compressed CSS files if they exist and the client accepts gzip.\n RewriteCond %{HTTP:Accept-encoding} gzip\n RewriteCond %{REQUEST_FILENAME}\\.gz -s\n RewriteRule ^(.*)\\.css \$1\\.css\\.gz [QSA]\n\n # Serve gzip compressed JS files if they exist and the client accepts gzip.\n RewriteCond %{HTTP:Accept-encoding} gzip\n RewriteCond %{REQUEST_FILENAME}\\.gz -s\n RewriteRule ^(.*)\\.js \$1\\.js\\.gz [QSA]\n\n # Serve correct content types, and prevent mod_deflate double gzip.\n RewriteRule \\.css\\.gz\$ - [T=text/css,E=no-gzip:1]\n RewriteRule \\.js\\.gz\$ - [T=text/javascript,E=no-gzip:1]\n\n <FilesMatch \"(\\.js\\.gz|\\.css\\.gz)\$\">\n # Serve correct encoding type.\n Header set Content-Encoding gzip\n # Force proxies to cache gzipped & non-gzipped css/js files separately.\n Header append Vary Accept-Encoding\n </FilesMatch>\n </IfModule>\n</IfModule>\n";
rename($htaccess_path, $htaccess_path . "-backup");
if (file_put_contents($htaccess_path, $htaccess_lines)) {
drupal_chmod($htaccess_path, 0444);
drupal_set_message("Old htaccess backup created at: " . $htaccess_path . "-backup");
}
else {
$variables = array(
'%directory' => $directory,
'!htaccess' => '<br />' . nl2br(check_plain($htaccess_lines)),
);
watchdog('security', "Security warning: Couldn't write .htaccess file. Please create a .htaccess file in your %directory directory which contains the following lines: <code>!htaccess</code>", $variables, WATCHDOG_ERROR);
}
}