我的檔案夾集如下所示:
server
--public
----index.php
----/items/
------index.php
------template.php
------/folder1/
------/folder2/
------/folder3/
--------index.php
------/folder4/
發生這種情況時,瀏覽器中的 folder3 會顯示 site.com/items/folder3/ 上的內容。
我的任務是在/folder1/、/folder2/、/folder4/ 中顯示檔案site.com/items/template.php 中的內容(因為這些檔案夾不包含index.php 或index.html)。我怎樣才能做到這一點?
============================
我嘗試過這種方式,但它有缺點 - 這些規則也適用于不存在的目錄(如 /items/aaa/bbb/zzz/),這對我來說是不合適的。
DirectoryIndex index.html index.htm index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1/index\.html !-f
RewriteCond %{DOCUMENT_ROOT}/$1/index\.htm !-f
RewriteCond %{DOCUMENT_ROOT}/$1/index\.php !-f
RewriteRule ^(. ?)(?:/[^/] )?/?$ template.php [L]
uj5u.com熱心網友回復:
您可以在這里找到答案 https://stackoverflow.com/a/22412283/2118611
您應該將空檔案夾重寫為 template.php
嘗試這樣的事情:
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to template.php
RewriteRule .* /items/template.php [L]
編輯:
要檢查索引檔案是否存在,您可以嘗試以下操作:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index\.php !-f
RewriteRule . /template.php [L]
上面的代碼
RewriteCond %{REQUEST_FILENAME} !-f檢查請求的 uri 是否不是檔案。
RewriteCond %{REQUEST_FILENAME} -d 檢查請求的 uri 是否是目錄。
RewriteCond %{REQUEST_FILENAME}/index\.php !-f 檢查是否找不到 index.php。
然后重定向到 /template.php
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/357519.html
