我的目錄結構如下:
.htaccess
api.php
template/index.html
呼叫domain.com/api/someendpoint時,請求被轉發到api.php。
# enable apache rewrite engine
RewriteEngine on
# Deliver the folder or file directly if it exists on the server
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !-d
# Push every request to api.php
RewriteRule ^(.*)$ api.php [QSA]
現在,在呼叫時domain.com,應將請求呼叫到index.html模板目錄中:
RewriteCond %{REQUEST_URI} ^$
RewriteRule . template/index.html [QSA]
這不起作用,始終顯示 directoryListening。
獲取的正確方法是index.html什么?
uj5u.com熱心網友回復:
RewriteCond %{REQUEST_URI} ^$ RewriteRule . template/index.html [QSA]
RewriteRule 模式(即.)不必要地匹配除了檔案根目錄之外的所有內容。而且REQUEST_URI服務器變數總是以斜杠開頭,所以條件永遠不會匹配。所以,基本上,規則是什么都不做
要僅重寫根目錄,您只需要一個規則.htaccess:
RewriteRule ^$ template/index.html [L]
理想情況下,這將在您現有的 API 重寫之前進行,盡管這并不重要,因為其他重寫不會重寫根目錄(由于限制條件)。
# Push every request to api.php RewriteRule ^(.*)$ api.php [QSA]
是否應該將所有內容都發送到api.php,或者只是/api/像您的示例中那樣僅將 URL 發送到?至少,您應該使用 量詞,而不是*,因為您想排除對檔案根的請求(目前,第三個條件是阻止對檔案根的請求被重寫)。
請注意,如果您的 URL 是表單/api/someendpoint并且您正在重寫,/api.php那么您需要確保在檔案頂部禁用 MultiViews .htaccess:
Options -MultiViews
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/439120.html
下一篇:php檔案的404路徑
