在 Apache 中,如何使用子文件夹 .htaccess 文件对 URL 的无斜线版本进行外部重定向
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您缺少替换字符串(第二个参数)上的slash前缀(
/
) - 以使替换词根相关。或更确切地说,/subfolder/
(由于此.htaccess
文件位于子文件夹中)。由于这是A 相对替换字符串(不从斜线或方案+主机名开始),因此目录prefix *1 (我假设这是我的添加IS/fs9e/username/sub/public/path/path/code>)(默认情况下 *2 ),导致错误的重定向。 (这对于内部重写是正确的,但不是外部重定向。)
应该这样:
请注意,您还缺少
重新写入 模式。 (这也否定了检查
request_uri
以斜线结束的先前条件的需求。)还请注意,此“重定向”应该在早期的“重写”之前进行。
*1 The directory-prefix is the absolute filesystem path of the location of the .htaccess file.
*2 替代方案是设置
rewriteBase/subfolter
- 但这会影响所有相对替换。您也可以使用环境变量仅将特定前缀应用于某些规则。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:
Note you were also missing the end-of-string anchor (
$
) on theRewriteRule
pattern. (This also negates the need for the preceding condition that checks thatREQUEST_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.