如何重写 htaccess 规则以重定向到 css 个文件

how to rewrite the htaccess rules to redirect to css files

目标是能够以这种方式访问​​ CSS 文件 /css/index.cssindex.php 文件中,但是 CSS 文件的真实路径是 ./resources/styles/index.css.

如果用户打开网站,例如https://whosebug.com/

它必须将用户重定向到 public/index.php 文件(入口点),但同时所有 CSS 文件(实际上位于 resources/styles/ 目录下, 它们必须可以使用此 url:https://whosebug.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 配置(从根目录),但是 returns 我 Not Found - 404 页面:

RewriteEngine on

RewriteBase /
RewriteCond %{REQUEST_URI} /resources/styles/.*
RewriteRule ^resources/styles/(.*)$ /css/ [QSA,L]
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/ [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 文件。

我试过阅读这篇文章:

您可以在站点根目录 .htaccess 中包含这些规则:

RewriteEngine On

# rewrite css files to their actual path
RewriteRule ^css/(.+\.css)$ resources/styles/ [L,NC]

# rewrite js files to their actual path
RewriteRule ^js/(.+\.js)$ resources/js/ [L,NC]

# write root to public/
RewriteRule ^$ public/ [L]

RewriteRule ^(?!resources/).* public/[=10=] [L,NC]