首页 > 解决方案 > 将这些重写器添加到 .htaccess 时无法访问资产

问题描述

在我的 php 项目中,我使用根相对路径来访问前端资产(js、css、图像),例如:

所以浏览器正在寻找的是

[文档根目录]/assets/javascripts/anyscript.js

将这些规则添加到 .htaccess 时

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^/]+)/?$ index.php?page=$1 [L]
RewriteRule ^([^/]+)/(.+)/?$ index.php?page=$1&actions=$2 [L]

#Can't get it going with that.
#RewriteRule ^(?:([^/]+)/)+([^/]+)/?$ index.php?page=$1&url=$2 [L]

我可以通过以下网址访问页面和控制器:

但不再是资产文件夹中的资产:

在浏览器控制台中检查请求时,请求的路径似乎是正确的,但是当他请求时我得到了 404 响应。

我需要更改什么才能访问位于其中的 js、css 和图像等资产

[文档根目录]/assets 文件夹?


编辑:

以下 URL-Schemes 需要重定向:

old => //localhost/index.php?page=signin
new => //localhost/signin

old => //localhost/index.php?page=signin&action=someController/someMethod
new => //localhost/signin/someController/someMethod

old => //localhost/index.php?page=signin&action=someController/someMethod/param1/param2
new => //localhost/signin/someController/someMethod/param1/param2

不确定如何解决这个问题 - 但还不需要解决:

old => //localhost/index.php?action=/someController/someMethod
new => //localhost/someController/someMethod

标签: regex.htaccessurl-rewriting

解决方案


对我来说,第三个示例 URL 最终为:

https://localhost/index.php?page=assets%26actions=javascripts/somejs.js

并且该 URL 无法正常工作。演示
目前尚不完全清楚哪些需要重定向,哪些不需要,但让我们试试这个:

^(?:([^/]+)/)+([^/]+)/?$

推荐阅读