2014-12-09 13 views
11

Il server integrato di PHP non fa uso di .htaccess? Ha senso, suppongo, poiché non si basa su Apache (?). In ogni caso è possibile dire al server di utilizzare questi file, può gestire riscritture di URL? Ho alcune porge in framework che si basano su questi file.PHP integrato nel server e riscritture .htaccess mod

APPLICATION_ENV=development php -S localhost:8000 -t public/

+0

'-s' php non è Apache, perché dovrebbe leggere qualsiasi file di configurazione di Apache? –

+0

puoi dire al server il tuo 'front controller' di' php -S ... public/index.php' –

+0

Yeh ma i file .htaccess sono comunemente usati nelle applicazioni quindi mi chiedevo se il server PHP li avesse gestiti. Non credo. Puntare a un front controller va bene, ma quando voglio riscrivere qualcosa come/news/view/205 url al momento non posso. – Martyn

risposta

5

Non è possibile gestire .htaccess utilizzando PHP web server incorporato (non fa affidamento su apache, è implementato entierly nel nucleo di PHP). Tuttavia, è possibile utilizzare lo script del router (descritto qui: http://php.net/manual/en/features.commandline.webserver.php).

E.g. php -S localhost -S localhost:8080 router.php

+0

quindi è necessario utilizzare .htaccess – unixmiah

+0

Non è possibile utilizzare .htaccess con il server integrato di PHP. Non è supportato Se vuoi usare .htaccess e non altro modo di configurare il tuo webserver, allora devi usare Apache o analizzare .htaccedere te stesso in PHP (che probabilmente sarebbe una cosa difficile) – Agares

+1

ahh che abbia senso. si, sono d'accordo. – unixmiah

6

Ecco il router che utilizzo per il webserver php integrato che serve risorse dal filesystem se esistono e altrimenti esegue una riscrittura in un file index.php.

Run utilizzando:

php -S localhost:8080 router.php 

router.php:

<?php 

chdir(__DIR__); 
$filePath = realpath(ltrim($_SERVER["REQUEST_URI"], '/')); 
if ($filePath && is_dir($filePath)){ 
    // attempt to find an index file 
    foreach (['index.php', 'index.html'] as $indexFile){ 
     if ($filePath = realpath($filePath . DIRECTORY_SEPARATOR . $indexFile)){ 
      break; 
     } 
    } 
} 
if ($filePath && is_file($filePath)) { 
    // 1. check that file is not outside of this directory for security 
    // 2. check for circular reference to router.php 
    // 3. don't serve dotfiles 
    if (strpos($filePath, __DIR__ . DIRECTORY_SEPARATOR) === 0 && 
     $filePath != __DIR__ . DIRECTORY_SEPARATOR . 'router.php' && 
     substr(basename($filePath), 0, 1) != '.' 
    ) { 
     if (strtolower(substr($filePath, -4)) == '.php') { 
      // php file; serve through interpreter 
      include $filePath; 
     } else { 
      // asset file; serve from filesystem 
      return false; 
     } 
    } else { 
     // disallowed file 
     header("HTTP/1.1 404 Not Found"); 
     echo "404 Not Found"; 
    } 
} else { 
    // rewrite to our index file 
    include __DIR__ . DIRECTORY_SEPARATOR . 'index.php'; 
}