mod_重写公共文件夹中的所有非文件查询到./index.php


mod_rewrite all non-files queries in public folder to ../index.php

我有以下文件夹逻辑:

.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时很有用,在生产中不需要,因为大多数网站直接服务于公共文件夹。

此请求与include '../index.php';文件的public/index.php相反的原因是,例如,Slim使用$_SERVER['PHP_SELF'],这会通过强制/public进入url来破坏路由。解决这个问题的一个肮脏的方法是更改vendor/slim/slim文件夹中的文件,该文件夹应该是一个相对较大的忘记它。

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

(.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不做任何事情。

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