我希望使用PHP的内置服务器开发一个Drupal7网站。我已经成功地在没有干净URL(例如index.php?q=/about/)的情况下运行了Drupal,但干净URL(如/about/)通常依赖于mod_rewrite或其等效程序。在我看到的文档中,您可以使用如下路由器文件运行PHP服务器:
php -S localhost:8000 routing.php
我应该在routing.php中放入什么来模拟mod_rewrite?
任务基本上是用PHP为router.php
文件编码Drupal的.htaccess。
这里有一个开始:
<?php
if (preg_match("/'.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl('.php)?|xtmpl)/", $_SERVER["REQUEST_URI"])) {
print "Error'n"; // File type is not allowed
} else
if (preg_match("/(^|'/)'./", $_SERVER["REQUEST_URI"])) {
return false; // Serve the request as-is
} else
if (file_exists($_SERVER["DOCUMENT_ROOT"] . $_SERVER["SCRIPT_NAME"])) {
return false;
} else {
// Feed everything else to Drupal via the "q" GET variable.
$_GET["q"]=$_SERVER["REQUEST_URI"];
include("index.php");
}
这应该被认为是阿尔法质量。它代表了对Drupal 7.14的.htaccess文件进行3分钟的遍历,跳过任何需要超过10秒思考的内容。:)
然而,它确实允许我启动Drupal的安装脚本,按照预期加载样式表、JS和图像,并使用Clean URL访问Drupal的页面。请注意,要在此环境中安装Drupal,我需要一个可能不会成为Drupal7一部分的补丁。
您现在可以使用以下命令更轻松地启动服务器:
drush runserver
我自己在寻找解决方案,在Drupal 8问题中找到了一个:
这对我来说很好,现在在我现有的Drupal 7安装中:
将其保存为.htrouter.php(或任何您想要的),并在Drupal根目录中运行:
php -S localhost:8080 .htrouter.php
<?php
/**
* @file
* The router.php for clean-urls when use PHP 5.4.0 built in webserver.
*
* Usage:
*
* php -S localhost:8888 .htrouter.php
*
*/
$url = parse_url($_SERVER["REQUEST_URI"]);
if (file_exists('.' . $url['path'])) {
// Serve the requested resource as-is.
return FALSE;
}
// Remove opener slash.
$_GET['q'] = substr($url['path'], 1);
include 'index.php';
(代码段构建自https://drupal.org/files/router-1543858-3.patch)