【发布时间】:2021-10-30 05:23:52
【问题描述】:
目标是能够在index.php文件中以/css/index.css这种方式访问CSS文件,但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 文件。
我已尝试阅读此帖子:
【问题讨论】:
标签: apache .htaccess mod-rewrite