Skip to content

Commit

Permalink
Provide an example .htaccess file after installation - fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminkott committed Mar 30, 2014
1 parent 72dbbae commit da841fe
Show file tree
Hide file tree
Showing 3 changed files with 354 additions and 0 deletions.
108 changes: 108 additions & 0 deletions Classes/Service/InstallService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
namespace BK2K\BootstrapPackage\Service;

/***************************************************************
*
* The MIT License (MIT)
*
* Copyright (c) 2014 Benjamin Kott, http://www.bk2k.info
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
***************************************************************/

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageQueue;

/**
* @author Benjamin Kott <[email protected]>
*/
class InstallService {

protected $extKey = 'bootstrap_package';

/**
* @param string $extension
*/
public function generateApacheHtaccess($extension = NULL){
if($extension == $this->extKey){
if(substr($_SERVER['SERVER_SOFTWARE'], 0, 6) === 'Apache'){
$this->createDefaultHtaccessFile();
}else{

/**
* Add Flashmessage that the system it not running on an apache webserver and the url rewritings must be handled manually
*/
$message = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
'The Bootstrap Package uses RealUrl to generate SEO friendly URLs by default, please take care of the URLs rewriting settings for your environment yourself.<br>'
. 'You can also deactivate RealUrl by changing your TypoScript setup to "<strong>config.tx_realurl_enable = 0</strong>".',
'TYPO3 is not running on an Apache-Webserver',
FlashMessage::WARNING,
TRUE
);
FlashMessageQueue::addMessage($message);
return;
}
}
}

/**
* Creates .htaccess file inside the root directory
*
* @param string $htaccessFile Path of .htaccess file
* @return void
*/
public function createDefaultHtaccessFile(){
$htaccessFile = GeneralUtility::getFileAbsFileName(".htaccess");

if(file_exists($htaccessFile)){

/**
* Add Flashmessage that there is already an .htaccess file and we are not going to override this.
*/
$message = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
'There is already an Apache .htaccess file in the root directory, please make sure that the url rewritings are set properly.<br>'
. 'An example configuration is located at: <strong>typo3conf/ext/bootstrap_package/Configuration/Apache/.htaccess</strong>',
'Apache .htaccess file already exists',
FlashMessage::NOTICE,
TRUE
);
FlashMessageQueue::addMessage($message);
return;
}

$htaccessContent = GeneralUtility::getUrl(ExtensionManagementUtility::extPath($this->extKey).'/Configuration/Apache/.htaccess');
GeneralUtility::writeFile($htaccessFile, $htaccessContent, TRUE);

/**
* Add Flashmessage that the example htaccess file was placed in the root directory
*/
$message = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
'For RealURL and optimization purposes an example .htaccess file was placed in your root directory. <br>'
. ' Please check if the RewriteBase correctly set for your environment. ',
'Apache example .htaccess was placed in the root directory.',
FlashMessage::OK,
TRUE
);
FlashMessageQueue::addMessage($message);
}

}
231 changes: 231 additions & 0 deletions Configuration/Apache/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
# ------------------------------------------------------------------------------
# | CORS-enabled images |
# ------------------------------------------------------------------------------
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
<FilesMatch "\.(gif|ico|jpe?g|png|svgz?|webp)$">
SetEnvIf Origin ":" IS_CORS
Header set Access-Control-Allow-Origin "*" env=IS_CORS
</FilesMatch>
</IfModule>
</IfModule>


# ------------------------------------------------------------------------------
# | Web fonts access |
# ------------------------------------------------------------------------------
<IfModule mod_headers.c>
<FilesMatch "\.(eot|font.css|otf|ttc|ttf|woff)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>


# ------------------------------------------------------------------------------
# | 404 error prevention for non-existing redirected folders |
# ------------------------------------------------------------------------------
Options -MultiViews


# ------------------------------------------------------------------------------
# | Force IE to render pages in the highest available mode in the various |
# ------------------------------------------------------------------------------
<IfModule mod_headers.c>
Header set X-UA-Compatible "IE=edge"
<FilesMatch "\.(appcache|crx|css|eot|gif|htc|ico|jpe?g|js|m4a|m4v|manifest|mp4|oex|oga|ogg|ogv|otf|pdf|png|safariextz|svgz?|ttf|vcf|webapp|webm|webp|woff|xml|xpi)$">
Header unset X-UA-Compatible
</FilesMatch>
</IfModule>


# ------------------------------------------------------------------------------
# | Proper MIME types for all files |
# ------------------------------------------------------------------------------
<IfModule mod_mime.c>

AddType audio/mp4 m4a f4a f4b
AddType audio/ogg oga ogg

AddType application/javascript js
AddType application/json json

AddType video/mp4 mp4 m4v f4v f4p
AddType video/ogg ogv
AddType video/webm webm
AddType video/x-flv flv

AddType application/font-woff woff
AddType application/vnd.ms-fontobject eot

AddType application/x-font-ttf ttc ttf
AddType font/opentype otf

AddType image/svg+xml svg svgz
AddEncoding gzip svgz

AddType application/octet-stream safariextz
AddType application/x-chrome-extension crx
AddType application/x-opera-extension oex
AddType application/x-shockwave-flash swf
AddType application/x-web-app-manifest+json webapp
AddType application/x-xpinstall xpi
AddType application/xml atom rdf rss xml
AddType image/webp webp
AddType image/x-icon ico
AddType text/cache-manifest appcache manifest
AddType text/vtt vtt
AddType text/x-component htc
AddType text/x-vcard vcf

</IfModule>


# ------------------------------------------------------------------------------
# | UTF-8 encoding |
# ------------------------------------------------------------------------------
AddDefaultCharset utf-8
<IfModule mod_mime.c>
AddCharset utf-8 .atom .css .js .json .rss .vtt .webapp .xml
</IfModule>


# ------------------------------------------------------------------------------
# | Rewrite engine |
# ------------------------------------------------------------------------------
<IfModule mod_rewrite.c>

Options +FollowSymlinks

# Enable URL rewriting
RewriteEngine On

# Change this path, if your TYPO3 installation is located in a subdirectory of the website root.
#RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ $1.$3 [L]
RewriteRule ^fileadmin/(.*/)?_recycler_/ - [F]
RewriteRule ^fileadmin/templates/.*(\.txt|\.ts)$ - [F]
RewriteRule ^typo3conf/ext/[^/]+/Resources/Private/ - [F]
RewriteRule ^(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]

</IfModule>


# ------------------------------------------------------------------------------
# | Block access to directories without a default document. |
# ------------------------------------------------------------------------------
<IfModule mod_autoindex.c>
Options -Indexes
</IfModule>


# ------------------------------------------------------------------------------
# | Block access to hidden files and directories. |
# ------------------------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\." - [F]
</IfModule>


# ------------------------------------------------------------------------------
# | lock access to backup and source files. |
# ------------------------------------------------------------------------------
<FilesMatch "(^#.*#|\.(bak|.ts|config|dist|fla|inc|ini|log|psd|sh|sql|sw[op])|~)$">
Order allow,deny
Deny from all
Satisfy All
</FilesMatch>


# ------------------------------------------------------------------------------
# | Compression |
# ------------------------------------------------------------------------------
<IfModule mod_deflate.c>
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
</IfModule>
</IfModule>
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE application/atom+xml \
application/javascript \
application/json \
application/rss+xml \
application/vnd.ms-fontobject \
application/x-font-ttf \
application/x-web-app-manifest+json \
application/xhtml+xml \
application/xml \
font/opentype \
image/svg+xml \
image/x-icon \
text/css \
text/html \
text/plain \
text/x-component \
text/xml
</IfModule>
</IfModule>


# ------------------------------------------------------------------------------
# | ETag removal |
# ------------------------------------------------------------------------------
<IfModule mod_headers.c>
Header unset ETag
</IfModule>
FileETag None


# ------------------------------------------------------------------------------
# | Expires headers (for better cache control) |
# ------------------------------------------------------------------------------
<IfModule mod_expires.c>

ExpiresActive on
ExpiresDefault "access plus 1 month"

ExpiresByType text/css "access plus 1 year"

ExpiresByType application/json "access plus 0 seconds"
ExpiresByType application/xml "access plus 0 seconds"
ExpiresByType text/xml "access plus 0 seconds"

ExpiresByType image/x-icon "access plus 1 week"

ExpiresByType text/x-component "access plus 1 month"

ExpiresByType text/html "access plus 0 seconds"

ExpiresByType application/javascript "access plus 1 year"

ExpiresByType application/x-web-app-manifest+json "access plus 0 seconds"
ExpiresByType text/cache-manifest "access plus 0 seconds"

ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/ogg "access plus 1 month"
ExpiresByType video/webm "access plus 1 month"

ExpiresByType application/atom+xml "access plus 1 hour"
ExpiresByType application/rss+xml "access plus 1 hour"

ExpiresByType application/font-woff "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
ExpiresByType application/x-font-ttf "access plus 1 month"
ExpiresByType font/opentype "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 month"

</IfModule>
15 changes: 15 additions & 0 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,19 @@
$settings = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$_EXTKEY]);
if($settings['UseRealUrlConfig'] == 1){
@include_once(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($_EXTKEY,'Configuration/RealURL/Default.php'));
}


if (TYPO3_MODE === 'BE') {

/**
* Provides an example .htaccess file for Apache after extension is installed and shows a warning if TYPO3 is not running on Apache.
*/
$signalSlotDispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\SignalSlot\\Dispatcher');
$signalSlotDispatcher->connect(
'TYPO3\\CMS\\Extensionmanager\\Service\\ExtensionManagementService',
'hasInstalledExtensions',
'BK2K\\BootstrapPackage\\Service\\InstallService',
'generateApacheHtaccess'
);
}

0 comments on commit da841fe

Please sign in to comment.