Use index.php for serving content

Removes main.php reliance. Paves the way for
serving SilverStripe from a public/ subfolder in the base path.
This requires an index.php file in the webroot,
since you can't set a RewriteRule to a path outside of the webroot
(e.g. "public/.htaccess" pointing to "../vendor/silverstripe/framework/main.php").
This commit is contained in:
Ingo Schommer 2017-10-03 15:43:14 +01:00
parent edf6f4c1d3
commit bce724ca76
2 changed files with 14 additions and 55 deletions

View File

@ -26,5 +26,5 @@
# Non existant files passed to requesthandler
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ../vendor/silverstripe/framework/main.php?url=%1 [QSA]
RewriteRule .* ../index.php [QSA]
</IfModule>

View File

@ -1,59 +1,18 @@
<?php
/************************************************************************************
************************************************************************************
** **
** If you can read this text in your browser then you don't have PHP installed. **
** Please install PHP 5.6.0 or higher. **
** **
************************************************************************************
************************************************************************************/
use SilverStripe\Control\HTTPApplication;
use SilverStripe\Control\HTTPRequestBuilder;
use SilverStripe\Core\CoreKernel;
use SilverStripe\Core\Startup\ErrorControlChainMiddleware;
/**
* This script bolts on top of SilverStripe to allow access without the use of .htaccess
* rewriting rules.
*/
require __DIR__ . '/vendor/autoload.php';
// This is the URL of the script that everything must be viewed with.
define('BASE_SCRIPT_URL', 'index.php/');
// Build request and detect flush
$request = HTTPRequestBuilder::createFromEnvironment();
$ruLen = strlen($_SERVER['REQUEST_URI']);
$snLen = strlen($_SERVER['SCRIPT_NAME']);
$isIIS = (strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false);
// IIS will populate server variables using one of these two ways
if ($isIIS) {
if ($_SERVER['REQUEST_URI'] == $_SERVER['SCRIPT_NAME']) {
$url = "";
} elseif ($ruLen > $snLen && substr($_SERVER['REQUEST_URI'], 0, $snLen + 1) == ($_SERVER['SCRIPT_NAME'] . '/')) {
$url = substr($_SERVER['REQUEST_URI'], $snLen + 1);
$url = strtok($url, '?');
} else {
$url = $_SERVER['REQUEST_URI'];
if ($url[0] == '/') {
$url = substr($url, 1);
}
$url = strtok($url, '?');
}
// Apache will populate the server variables this way
} else {
if ($ruLen > $snLen && substr($_SERVER['REQUEST_URI'], 0, $snLen + 1) == ($_SERVER['SCRIPT_NAME'] . '/')) {
$url = substr($_SERVER['REQUEST_URI'], $snLen + 1);
$url = strtok($url, '?');
} else {
$url = "";
}
}
$_GET['url'] = $_REQUEST['url'] = $url;
$fileName = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . $url;
// Pass through references to existing files
if ($url && file_exists($fileName)) {
return false;
}
require_once 'vendor/silverstripe/framework/main.php';
// Default application
$kernel = new CoreKernel(BASE_PATH);
$app = new HTTPApplication($kernel);
$app->addMiddleware(new ErrorControlChainMiddleware($app));
$response = $app->handle($request);
$response->output();