首页 > 解决方案 > 条件 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 位于/appand/staging文件夹中(但它必须是同一个文件)

标签: apache.htaccess

解决方案


在暂存文件夹的 htaccess 文件中,将所有实例更改appstaging

编辑:您可以尝试使用相对路径而不是硬编码应用程序或登台,但我过去曾遇到过问题,特别是如果还有其他规则在起作用。

RewriteEngine On 

RewriteRule ^dist -  [NC,L] 

RewriteRule ^$ dist/ [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ dist/$1 [L]

这个想法是从重写中删除前导 / 以便使用相对路径。因此,从技术上讲,htaccess 文件所在的任何文件夹都无关紧要


推荐阅读