【问题标题】:Possible ? Can i using "PHP -S localhost" reading with .htaccess?可能的 ?我可以使用.htaccess 读取“PHP -S localhost”吗?
【发布时间】:2021-08-24 16:16:35
【问题描述】:

我可以在命令提示符中使用一些特殊文本来读取带有php -S 服务内置在我的项目中的.htaccess 文件

例如:~/project> $ php -S localhost:8000 -read .htaccess 在我的项目中检查变量 $_GET['url'] 用于控制路由重定向到某个文件。

可能的方式?

我的小项目:

  • 这是我的项目目录
project
   |- src
   |- index.php
   |- .htaccess
  • .htaccess 文件中的代码:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>

  • index.php 文件中的代码:
<?php

$url = $_GET['url'];

if ($url == "hello") {

    // go to hello file

} else {

    // go to other file

}

?>

【问题讨论】:

  • 对物理上不存在的文件的请求会自动路由到当前文件夹中的 index.php 或 index.html,如果都没有找到,它会继续检查父文件夹(参见 php.net/manual/en/features.commandline.webserver.php ) 你不会自动获得 GET 参数,你必须在 $_SERVER 中找到最初请求的 URL。
  • 如果你实现了上面的,你可以在以后使用相同的逻辑,当整个事情在一个“真实的”网络服务器上运行时,使用 .htaccess 文件/可以重写。只需在没有任何参数的情况下重写 index.php,或者改用FallbackResource 指令。

标签: php .htaccess web-services


【解决方案1】:

htaccess 特定于 Apache 服务器。它不适用于 PHP 内置的网络服务器。

【讨论】:

  • 你有办法吗?为了解决这个问题?通过没有 .htaccess 文件
  • 在 PHP 中你可以set a Location header to go to another file。但是……你为什么不直接使用 Apache 呢?当您使您的网站上线时,您将不会使用 PHP 内置服务器。如果您也使用 Apache 进行测试,您将更准确地再现实时系统的工作方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多