例如,考慮 apache2 在 http://localhost/htaccess-test 上提供的頁面。
該檔案夾可能如下所示:
htaccess-test
|- .htaccess
|- index.php
這是.htaccess:
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, PATCH, OPTIONS"
RewriteEngine on
RewriteRule ^ index.php [QSA,L]
每個呼叫都被重定向到 index.php 并由 PHP(或其他)從那里處理。
現在在不同的埠或主機上生成客戶端:
<html>
<body>
<script>
(function() {
const xhttp = new XMLHttpRequest();
xhttp.open("PUT", "http://localhost/htaccess-test/foo");
xhttp.send();
})();
</script>
</body>
</html>
作品。但是,如果您將檔案夾 'foo' 添加到頁面的檔案夾 ('htaccess-test'),呼叫會導致 CORS 錯誤!
誰能幫我避免這種情況!我有一個名為“test”的 Endpojnt,它與測驗檔案夾沖突......
uj5u.com熱心網友回復:
xhttp.open("PUT", "http://localhost/htaccess-test/foo");
如果/foo是物理目錄,則 mod_dir 將發出 301 重定向以附加尾部斜杠。重定向回應不會設定 CORS 標頭,因為您沒有將always條件與Header指令一起使用,因此會出現 CORS 錯誤。
但是,我希望您首先不想要尾部斜杠,因此您需要防止 mod_dir 附加尾部斜杠。
在檔案頂部添加以下內容.htaccess:
# Prevent mod_dir appending a trailing slash to directories
DirectorySlash Off
但是,由于您要禁用目錄斜杠,因此您需要確保目錄串列也被禁用,因為 DirectoryIndex 檔案(在該目錄中)的存在將不再阻止 mod_autoindex 生成目錄串列(如果省略了尾部斜杠) . 在檔案頂部添加以下內容.htaccess(如果您還沒有):
# Prevent mod_autoindex from generating directory listings.
Options -Indexes
如果您需要直接訪問任何目錄,則需要根據需要手動附加尾部斜杠(使用外部重定向或內部重寫)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/459210.html
