【问题标题】:URL rewrite all php files in subfolder to index.phpURL 将子文件夹中的所有 php 文件重写为 index.php
【发布时间】:2015-05-30 08:05:09
【问题描述】:

我正在尝试设置一种方法来将子文件夹中的所有 php 文件重写为 index.php

到目前为止我一直在尝试的事情:

RewriteEngine on

RewriteBase /simple

RewriteRule ^(.+)/$ index.php

它不能 100% 工作,现在它也需要 CSS、IMG 和所有其他文件,它不应该,只有 PHP 文件。

希望有人能提供帮助。 谢谢。

【问题讨论】:

    标签: php apache .htaccess url rewrite


    【解决方案1】:

    RewriteRule ^(.+)\.php$ index.php 是方式

    【讨论】:

    • 除了 .css、.png、jpg 等之外,是否可以将所有内容发送到 index.php。
    【解决方案2】:

    你可以使用:

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

    或者这条规则:

    RewriteRule !\.(js|ico|gif|jpe?g|png|css|html|swf|flv|xml)$ index.php [L,NC]
    

    【讨论】:

    • 它确实可以正常工作,只是它无法加载 .css 文件或图像。
    • 除了预配置的 .css、.png、jpg 等文件外,是否可以将所有文件发送到 index.php
    • 为此,您可以在页面 HTML 的 <head> 部分添加:<base href="/" />,以便每个 相对 URL 都从该基本 URL 而不是从当前页面的 URL。
    • 我刚刚通过谷歌搜索发现了这个:RewriteRule !\.(js|ico|gif|jpg|png|css|html|swf|flv|xml)$ index.php [QSA, L] - 我该如何实现呢?
    • 实际上不需要,因为我已经有RewriteCond %{REQUEST_FILENAME} !-f。正如我上面写的,你已经使用了<base...> 标签。
    猜你喜欢
    • 2017-12-24
    • 2016-06-24
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多