在 Apache 中,如何使用子文件夹 .htaccess 文件对 URL 的无斜线版本进行外部重定向

发布于 2025-01-19 10:37:23 字数 1039 浏览 1 评论 0原文

On Apache 2.4 I have an .htaccess (in a subfolder) which rewrites slashless requests inside that folder to appropriate index files:

DirectorySlash Off

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule (.*) $1/index.html [L]

This works for the slashless version exactly as expected.现在,我想将外部斜率的版本重定向到无斜面版本。 I tried adding the lines:

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} /$
RewriteRule ^(.*)/ $1 [R=302,L]

However this does not work: The redirect is issued, however it does not go to the slashless URL, but to a URL with a system specific part injected.

因此,对于示例URL http://example.com/path/to/dir/重定向的URL看起来像http://example.com/fs9e/username/sub/ public/path/to/dir而不是http://example.com/path/to/dir

我该如何解决?非常感谢任何指示!


PS:真实情况更为复杂,因为我在root .htacces中进行了子域对折叠器的重写,但我认为这在这里与之无关。

On Apache 2.4 I have an .htaccess (in a subfolder) which rewrites slashless requests inside that folder to appropriate index files:

DirectorySlash Off

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule (.*) $1/index.html [L]

This works for the slashless version exactly as expected. Now I want to redirect the slashed version externally to the slashless version. I tried adding the lines:

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} /$
RewriteRule ^(.*)/ $1 [R=302,L]

However this does not work: The redirect is issued, however it does not go to the slashless URL, but to a URL with a system specific part injected.

So, for a sample URL http://example.com/path/to/dir/ the redirected URL looks like this http://example.com/fs9e/username/sub/public/path/to/dir instead of just http://example.com/path/to/dir.

How can I fix this? Many thanks for any pointers!


PS: The real case is a little bit more complicated because I do a subdomain-to-folder rewrite in the root .htacces, but I assume this is not relevant here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆里有你的影子 2025-01-26 10:37:23
 重写 ^(。*)/ $ 1 [r = 302,l]
 

您缺少替换字符串(第二个参数)上的slash前缀(/) - 以使替换词根相关。或更确切地说,/subfolder/(由于此.htaccess文件位于子文件夹中)。由于这是A 相对替换字符串(不从斜线或方案+主机名开始),因此目录prefix *1 (我假设这是我的添加IS /fs9e/username/sub/public/path/path/code>)(默认情况下 *2 ),导致错误的重定向。 (这对于内部重写是正确的,但不是外部重定向。)

应该这样:

RewriteRule ^(.*)/$ /subfolder/$1 [R=302,L]

请注意,您还缺少重新写入 模式。 (这也否定了检查request_uri以斜线结束的先前条件的需求。)

还请注意,此“重定向”应该在早期的“重写”之前进行。



*1 The directory-prefix is the absolute filesystem path of the location of the .htaccess file.

*2 替代方案是设置rewriteBase/subfolter - 但这会影响所有相对替换。您也可以使用环境变量仅将特定前缀应用于某些规则。

RewriteRule ^(.*)/ $1 [R=302,L]

You are missing the slash prefix (/) on the substitution string (2nd argument) - to make the substitution root-relative. Or rather, /subfolder/ (since this .htaccess file is located in a subfolder). Since this is a relative substitution string (not starting with a slash or scheme+hostname), the directory-prefix*1 (which I assume is /fs9e/username/sub/public/path/) is added back (by default*2), resulting in a malformed redirect. (This is correct for internal rewrites, but not external redirects.)

It should be like this:

RewriteRule ^(.*)/$ /subfolder/$1 [R=302,L]

Note you were also missing the end-of-string anchor ($) on the RewriteRule pattern. (This also negates the need for the preceding condition that checks that REQUEST_URI ends in a slash.)

Note also that this "redirect" should go before the earlier "rewrite".



*1 The directory-prefix is the absolute filesystem path of the location of the .htaccess file.

*2 The alternative is to set a RewriteBase /subfolder - but that then affects all relative substitutions. You could also use an environment variable to apply a specific prefix only to some rules.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文