Ric*_*984 9 linux mod-rewrite .htaccess apache-2.2 apache-2.4
我的 htaccess 中有这个,但不知道它的用途。由于规则的性质,搜索也无济于事。
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301]
Run Code Online (Sandbox Code Playgroud)
谁能解释一下它的用途是什么?
Sve*_*ven 11
这个规则所做的就是在/你的 URLS 中添加一个尾随,如果没有,如果.URI 中没有,那么https://example.org/test将被重定向到https://example.org/test/,但https://example.org/test.html不会被重写https://example.org/test.html/(但注意:https://example.org/test.case/folder也不会被重定向到,https://example.org/test.case/folder/因为它包含一个.在URI)。
## Do the following if the URI does not end with `/` or does not contain an `.`:
## the . is relevant for file names like test.html, which should n
RewriteCond %{REQUEST_URI} !(/$|\.)
## Redirect it to the original URI with an added `/` and mark this as permanent:
RewriteRule (.*) %{REQUEST_URI}/ [R=301]
Run Code Online (Sandbox Code Playgroud)
未经验证,但根据我在 Apache 重写方面的经验,此配置似乎:
这将导致以下测试用例
/ -> /
/test -> /test/
/my/resource -> /my/resource/
/my/resource.type -> /my/resource.type
/edge.case/resource -> /edge.case/resource
所以我认为该规则的目的是为似乎不是文件的资源添加斜杠,但它似乎有一个边缘情况。
如果没有向带有“.”的资源添加斜杠。路径的非文件部分中的(点)字符,正则表达式应更改为:
# match paths which do not end with a slash, or do not resemble a file with an extension
RewriteCond %{REQUEST_URI} !(/$|\.[^/]+$)
# redirect permanently to the same uri with a slash
RewriteRule (.*) %{REQUEST_URI}/ [R=301]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
392 次 |
| 最近记录: |