【问题标题】:modrewrite how can all files to index.php?mod_rewrite 怎么能把所有文件都转成index.php?
【发布时间】:2022-02-06 17:43:04
【问题描述】:

实际的.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule index.php [L]

目标: 所有文件都应该指向 index.php 还有 png/xml 等。

在浏览器上调用: http://localhost:8080/test.png

浏览器消息: enter image description here

日志:

[Sun Feb  6 01:23:29 2022] [::1]:57052 Accepted
[Sun Feb  6 01:23:29 2022] [::1]:57052 [404]: GET /test.png - No such file or directory
[Sun Feb  6 01:23:29 2022] [::1]:57052 Closing

我的服务器开始测试:

php -S localhost:8080 -dxdebug.mode=debug -dxdebug.client_host=127.0.0.1 -dxdebug.client_port=9003 -dxdebug.start_with_request=yes

我的实际服务器:

PHP 8.1.1 (cli) (built: Dec 17 2021 22:38:05) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.1, Copyright (c) Zend Technologies
    with Xdebug v3.1.2, Copyright (c) 2002-2021, by Derick Rethans
    with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies

适用于 macbook ...

有想法解决我的问题吗?

【问题讨论】:

    标签: .htaccess webserver


    【解决方案1】:

    PHP 8.1.1 (cli)(构建时间:2021 年 12 月 17 日 22:38:05)(NTS)

    您正在使用内置的 PHP 网络服务器。 .htaccess(以及扩展名为 mod_rewrite)是一个 Apache 配置文件,不受 PHP 网络服务器的支持。

    要使用 PHP 内置网络服务器实现前端控制器/路由器,您需要使用 PHP,如 PHP 文档中所述:


    除此之外: mod_rewrite 规则也是不正确的,不会像写的那样工作:

    RewriteRule index.php [L]
    

    您缺少RewriteRule 指令的第一个参数,因此它永远不会匹配您发出的请求。应该是这样的:

    RewriteRule . index.php [L]
    

    其中.(一个点)是一个正则表达式(pattern),它匹配至少一个字符(即除文档根之外的所有字符,由DirectoryIndex 处理) .

    进一步参考:


    适用于 MacBook ...

    您必须使用 Apache,使用不同的 .htaccess 文件!

    【讨论】:

      猜你喜欢
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      相关资源
      最近更新 更多