使用 .htaccess 导航到 URL 中最后一个目录的父目录
Using .htaccess to navigate to parent directory of last directory in URL
我正在尝试编写将忽略 URL 的最终子目录的 .htaccess 代码,路由到该最终子目录之前的目录,但保持 URL 不变。
这是在我使用 JavaScript 进行路由的 SPA 上下文中。
如果我导航到目录 xxx,它可以任意深度嵌套:
https://my-site/[subdirectories]/xxx
...那么当点击xxx目录下的相关链接时,如:
./aaa
./bbb
./ccc
...这将对应于(并产生 URLs):
https://my-site/[subdirectories]/xxx/aaa
https://my-site/[subdirectories]/xxx/bbb
https://my-site/[subdirectories]/xxx/ccc
...然后 JavaScript 将检测 URL 中的变化并显示 "virtual" 子目录 aaa
、bbb
的内容, ccc
.很好很花花公子。
但是,如果我直接转到这些 URL 中的任何一个,即直接输入 URL,而不是在网站内使用导航,那么我的网站会抛出 404 错误,因为 aaa
、bbb
和 ccc
不是实际的子目录。它们是 "virtual" 个子目录,与服务器上的实际文件夹不对应,因此抛出错误。
我想编写一个 .htaccess 例程来检测初始导航(直接打开):
https://my-site/[subdirectories]/xxx/aaa
https://my-site/[subdirectories]/xxx/bbb
https://my-site/[subdirectories]/xxx/ccc
等等
...然后只需打开此位置:
https://my-site/[subdirectories]/xxx
...但在 URL 中保留末尾的 "virtual" 子目录(aaa
、bbb
、ccc
),如URL吧。我的 JavaScript 将从那里接管,为 aaa
、bbb
、ccc
引入所需的内容。 (在第一次直接导航到 https://my-site/[subdirectories]/xxx
然后简单地单击指向 aaa
、bbb
、ccc
的链接后,这工作正常;它将转到任何 "subdirectories"直接就是问题所在。)
这可以用 .htaccess 完成吗?我在挣扎。
诀窍是制定一个重写规则,为目标内容指定一个文件。
如果子目录名称已知,在本例中它们是 aaa
、bbb
和 ccc
,则以下规则将打开 index.html 文件保留 URL 完整的子目录:
RewriteEngine on
# If it's not an actual directory...
RewriteCond %{REQUEST_FILENAME} !-d
# If it's not an actual file...
RewriteCond %{REQUEST_FILENAME} !-f
# Provided aaa, bbb, and ccc are the known subdirectories,
# and (.+) represents ANY text that follows (i.e., any deeper
# subdirectory structure), then /index.html will route to
# the index.html file in whichever folder of the aaa|bbb|ccc list
# that is provided:
RewriteRule ^(aaa|bbb|ccc)/(.+)$ /index.html [L]
我正在尝试编写将忽略 URL 的最终子目录的 .htaccess 代码,路由到该最终子目录之前的目录,但保持 URL 不变。
这是在我使用 JavaScript 进行路由的 SPA 上下文中。
如果我导航到目录 xxx,它可以任意深度嵌套:
https://my-site/[subdirectories]/xxx
...那么当点击xxx目录下的相关链接时,如:
./aaa
./bbb
./ccc
...这将对应于(并产生 URLs):
https://my-site/[subdirectories]/xxx/aaa
https://my-site/[subdirectories]/xxx/bbb
https://my-site/[subdirectories]/xxx/ccc
...然后 JavaScript 将检测 URL 中的变化并显示 "virtual" 子目录 aaa
、bbb
的内容, ccc
.很好很花花公子。
但是,如果我直接转到这些 URL 中的任何一个,即直接输入 URL,而不是在网站内使用导航,那么我的网站会抛出 404 错误,因为 aaa
、bbb
和 ccc
不是实际的子目录。它们是 "virtual" 个子目录,与服务器上的实际文件夹不对应,因此抛出错误。
我想编写一个 .htaccess 例程来检测初始导航(直接打开):
https://my-site/[subdirectories]/xxx/aaa
https://my-site/[subdirectories]/xxx/bbb
https://my-site/[subdirectories]/xxx/ccc
等等
...然后只需打开此位置:
https://my-site/[subdirectories]/xxx
...但在 URL 中保留末尾的 "virtual" 子目录(aaa
、bbb
、ccc
),如URL吧。我的 JavaScript 将从那里接管,为 aaa
、bbb
、ccc
引入所需的内容。 (在第一次直接导航到 https://my-site/[subdirectories]/xxx
然后简单地单击指向 aaa
、bbb
、ccc
的链接后,这工作正常;它将转到任何 "subdirectories"直接就是问题所在。)
这可以用 .htaccess 完成吗?我在挣扎。
诀窍是制定一个重写规则,为目标内容指定一个文件。
如果子目录名称已知,在本例中它们是 aaa
、bbb
和 ccc
,则以下规则将打开 index.html 文件保留 URL 完整的子目录:
RewriteEngine on
# If it's not an actual directory...
RewriteCond %{REQUEST_FILENAME} !-d
# If it's not an actual file...
RewriteCond %{REQUEST_FILENAME} !-f
# Provided aaa, bbb, and ccc are the known subdirectories,
# and (.+) represents ANY text that follows (i.e., any deeper
# subdirectory structure), then /index.html will route to
# the index.html file in whichever folder of the aaa|bbb|ccc list
# that is provided:
RewriteRule ^(aaa|bbb|ccc)/(.+)$ /index.html [L]