我們的目標是能夠訪問CSS檔案這種方式/css/index.css中index.php的檔案,但對CSS檔案的真實路徑是./resources/styles/index.css。
如果用戶打開網站,例如 https://stackoverflow.com/
它必須將用戶重定向到public/index.php檔案(入口點),但同時所有 CSS 檔案(實際上都位于resources/styles/目錄下,它們必須可以使用這個 url: 訪問https://stackoverflow.com/css/index.css,并相應地應用于 HTML 頁面。
我目前的專案結構:
public/
├─ .htaccess
├─ index.php
resources/
├─ styles/
│ ├─ index.css
│ ├─ main.css
.htaccess
我的./public/index.php檔案(必須是入口點):
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="/css/index.css">
{{-- but the actual path to index.css is "./resources/styles/index.css"--}}
</head>
<body>
...
</body>
</html>
我已經嘗試過這個.htaccess配置(從根目錄),但它回傳了我的Not Found - 404頁面:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} /resources/styles/.*
RewriteRule ^resources/styles/(.*)$ /css/$1 [QSA,L]
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
我在這里想念什么?如果可能,請用一些額外的細節描述它。
我的./public/.htaccess配置:
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
另外,對這些( [js, fonts, images]) 型別執行相同操作的最佳方法是什么?
我知道這可以通過這種方式在 apache 配置中完成:
...
Alias /css "/var/www/html/resources/styles"
<Directory /var/www/html/resources/styles>
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
...
如果 apache 配置中缺少某些內容,請糾正我。
但是,目前,我需要能夠使用該.htaccess檔案。
我試過閱讀這篇文章:
- htacess 重定向到 js 和 css 檔案;
- htaccess RewriteRule 重定向到父目錄?
- htaccess 也重定向 css、js 檔案
uj5u.com熱心網友回復:
您可以在站點根目錄 .htaccess 中設定這些規則:
RewriteEngine On
# rewrite css files to their actual path
RewriteRule ^css/(. \.css)$ resources/styles/$1 [L,NC]
# rewrite js files to their actual path
RewriteRule ^js/(. \.js)$ resources/js/$1 [L,NC]
# write root to public/
RewriteRule ^$ public/ [L]
RewriteRule ^(?!resources/).* public/$0 [L,NC]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/344855.html
上一篇:奇怪的重寫規則導致錯誤
