regex - 将这些重写器添加到 .htaccess 时无法访问资产
问题描述
在我的 php 项目中,我使用根相对路径来访问前端资产(js、css、图像),例如:
- /assets/css/styles.css
- /assets/javascripts/anyscript.js
所以浏览器正在寻找的是
[文档根目录]/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]
我可以通过以下网址访问页面和控制器:
- //本地主机/登录/
- //localhost/signin/sessionController/testingMethod
- //localhost/signin/sessionController/testingMethod/parameter1/parameter2
但不再是资产文件夹中的资产:
- //localhost/assets/javascripts/somejs.js?v=123
在浏览器控制台中检查请求时,请求的路径似乎是正确的,但是当他请求时我得到了 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
解决方案
对我来说,第三个示例 URL 最终为:
https://localhost/index.php?page=assets%26actions=javascripts/somejs.js
并且该 URL 无法正常工作。演示。
目前尚不完全清楚哪些需要重定向,哪些不需要,但让我们试试这个:
^(?:([^/]+)/)+([^/]+)/?$
推荐阅读
- javascript - div 自动滑动淡入/淡出或轻松实现更好的过渡
- java - Android getOrientation() 方位角结果与文档不匹配
- python - 成对读取多个 CSV 文件,它们比较每两对
- python - 尝试与作用域通信时出错,VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed
- html - 如何在图像顶部显示菜单,标题在图像中间?
- amazon-web-services - 通过 AWS SES 发送的附件在 iphone 上不可见
- python-3.x - 对numpy返回的特征向量感到困惑
- python - 如何使用 lambda 将数组中的每一项相乘,但每一项都有不同的操作
- sqlite - sqlite 使用 blob 作为纪元日期时间
- android - Ionic 4加载本地资源在IOS上不起作用