【问题标题】:mod_rewrite all non-files queries in public folder to ../index.phpmod_rewrite 公用文件夹中的所有非文件查询到 ../index.php
【发布时间】:2015-08-20 18:13:03
【问题描述】:

我有以下文件夹逻辑:

.htaccess
index.php
composer.json
(etc. with all processing files)
public/
    .htaccess
    design/
    files/
    (etc. with all static files)

在 .htaccess 中包含以下内容:

(.htaccess:)
RewriteEngine on
RewriteRule   ^(.*)$ public/$1    [L]
(public/.htaccess)
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

第一个 .htaccess 在 MAMP 上进行开发时很有用,而在生产中不需要请求,因为大多数网站直接为公共文件夹提供服务。

public/index.php 文件与include '../index.php'; 相比,此请求的原因是,例如,Slim 使用$_SERVER['PHP_SELF'] 会通过强制 /public 进入 url 来破坏路由。 One dirty way to solve this涉及更改vendor/slim/slim文件夹中的一个文件,应该是比较大的算了。

【问题讨论】:

    标签: php apache .htaccess mod-rewrite slim


    【解决方案1】:

    (在写我的问题时,我最终发现了一个我想分享的舒适解决方案)

    (.htaccess)
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !index.php
    RewriteRule   ^(.*)$ public/$1    [L]
    (public/.htaccess)
    RewriteEngine on
    RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ ../index.php/$1 [L,QSA]
    

    在开发中,

    • 第一个 .htaccess 将除 index.php 之外的所有调用重定向到 public/
    • 第二个 .htaccess 将所有非公开文件重定向回 ../index.php,
    • 第一个 .htaccess 不执行任何操作。

    在生产中,只完成第二步和第三步。

    这样,动态文件与静态文件严格分开。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 2012-03-08
      相关资源
      最近更新 更多