apache - 条件 URL 重写
问题描述
example.com/app
我需要显示内容的 URLexample.com/app/dist
下面的代码正是这样做的。
RewriteEngine On
RewriteRule ^$ /app/dist/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/dist/
RewriteRule ^(.*)$ /app/dist/$1
example.com/staging
但我还需要显示内容的 URLexample.com/staging/dist
使用当前设置,网址example.com/staging
显示的内容来自example.com/app/dist
.htaccess while 位于/app
and/staging
文件夹中(但它必须是同一个文件)
解决方案
在暂存文件夹的 htaccess 文件中,将所有实例更改app
为staging
编辑:您可以尝试使用相对路径而不是硬编码应用程序或登台,但我过去曾遇到过问题,特别是如果还有其他规则在起作用。
RewriteEngine On
RewriteRule ^dist - [NC,L]
RewriteRule ^$ dist/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ dist/$1 [L]
这个想法是从重写中删除前导 / 以便使用相对路径。因此,从技术上讲,htaccess 文件所在的任何文件夹都无关紧要
推荐阅读
- node.js - 不解的猫鼬
- javascript - 可以在第一次渲染时映射,但在刷新显示时无法映射未定义
- json - 在没有未来/异步的情况下解析本地 .json 同步?
- java - 测试黄瓜覆盖率
- macos - 我可以将 MacOS cmd 键映射到 Kitty 终端上的 ctrl 键吗?
- arrays - 将 JSON 转换为数组 Typescript
- node.js - Typescript ESLint 声明文件
- javascript - 为什么要删除输入类型=“数字”中的增加/减少数字?
- clickhouse - ClickHouse 是否支持地图字段?
- html - SQL 仅存储当前日期而不是选定日期