簡體   English   中英

mod_rewrite公用文件夾中的所有非文件查詢到../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]

當在MAMP上進行開發並且在生產中不需要時,第一個.htaccess很有用,因為大多數網站直接為公用文件夾提供服務。

提出此請求的原因,而不是使用include '../index.php';public/index.php文件include '../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是將所有非文件重定向回public之外的../index.php,
  • 第一個.htaccess不執行任何操作。

在生產中,僅執行第二步和第三步。

這樣,動態文件與靜態文件嚴格分開。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM