使用 htaccess 删除目录后的尾部斜杠

发布于 2025-01-09 05:52:35 字数 474 浏览 1 评论 0原文

当我想访问子目录文件夹中的索引文件时,我想删除 / 。例如:www.example.com/test/dashboard/www.example.com/test/dashboard

我尝试了这个:

RewriteEngine On

# Remove "/" to "/dashboard"
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule (.*) $1%1/ [L]

它不会从子目录中删除 /

您能否向我展示一个示例,说明当我想要访问子目录时如何使用 .htaccess 删除 /

I want to remove / when I want to get access to the index file in a subdirectory folder. For example: www.example.com/test/dashboard/ to www.example.com/test/dashboard.

I tried this:

RewriteEngine On

# Remove "/" to "/dashboard"
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule (.*) $1%1/ [L]

It will not remove the / from the subdirectory.

Can you please show me an example of how I can remove the / with .htaccess when I want to get access to my subdirectory?

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

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

发布评论

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

评论(1

狂之美人 2025-01-16 05:52:35
# 将“/”删除到“/dashboard”
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} ^(.*)$
重写规则 (.*) $1%1/ [L]

这不会“删除”任何东西。事实上,它会在 URL 路径和查询字符串的末尾附加一个斜杠,这看起来有点随机?

但是,您不能简单地删除 URL 路径中物理目录后面的尾部斜杠,因为 mod_dir 会尝试在其后面附加 301 重定向,以便“修复”URL。

您可以使用 DirectorySlash Off 指令阻止 mod_dir 附加尾部斜杠。但是,您随后需要通过内部重写手动将尾部斜杠附加到目录中,以便正确提供“索引文件”(即 DirectoryIndex 文档)。

我假设您链接到的目录在内部链接中没有尾部斜杠。

请在 root .htaccess 文件中尝试以下操作:

# Disable directory listings (mod_autoindex)
Options -Indexes

# Prevent mod_dir appending trailing slash to directories
DirectorySlash Off

RewriteEngine On

# Rewrite the URL to append a trailing slash to directories (internally)
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*[^/])$ $1/ [L]

映射到物理目录的 /dashboard(无尾部斜杠)请求将是内部重写为 /dashboard/,这将允许提供“索引文件”(也由 mod_dir 提供)。

出于安全原因,您需要确保禁用目录列表 (mod_autoindex),否则,即使目录包含目录索引文档,也可能会为目录生成目录列表。请参阅 下的 Apache 文档中的安全警告 >DirectorySlash 指令。

您需要确保在测试之前清除浏览器缓存,因为 mod_dir (附加尾部斜杠)进行的 301(永久)重定向肯定会被浏览器缓存。

删除尾部斜杠(可选)

如果第三方(或搜索引擎)发出任何包含尾部斜杠的请求,您可以实施规范重定向来实际从 URL 中“删除”尾部斜杠。 (它应该已经在您的所有内部链接上删除,因此这不是使您的网站“正常工作”所必需的,但是,SEO 可能需要它以避免潜在的重复内容。)

我假设您不希望任何 URL 上有尾部斜杠。

您应该在上面的 rewrite 之前添加以下“redirect”,紧接在 RewriteEngine 指令之后。

# Remove the trailing slash, should it appear on any 3rd party requests
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.+)/$ /$1 [R=301,L]

对 REDIRECT_STATUS 环境变量的检查是为了确保我们不会在重写引擎的第二次传递期间通过稍后的重写重定向已写入的请求(附加尾部斜杠)。或者,您可以在稍后重写时使用 END 标志 (Apache 2.4)。


更新:

.htaccess 文件不在根目录中

上面假设 .htaccess 文件位于文档根目录中(因此适用于 < em>所有目录及其子目录)。但是,如果 .htaccess 文件位于子目录中,那么您将需要修改外部重定向(即“删除”尾部斜杠),因为 RewriteRule pattern 将 URL 路径相对匹配到包含 .htaccess 文件的目录,而不是根目录。

因此,如果 .htaccess 文件位于子目录中,则使用以下“重定向”:

# Remove the trailing slash, should it appear on any 3rd party requests
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteRule /$ /%1 [R=301,L]

这需要一个额外的条件RewriteCond指令)从 REQUEST_URI 服务器变量(包含完整的根相对 URL 路径)捕获 URL 路径的相关部分,而不是 RewriteRule 模式,这将省略父目录。 %1 反向引用(与 $1 相对)引用从前面的条件RewriteCond 指令)捕获的子模式,而不是 RewriteRule 模式

顺便说一句,如果 .htaccess 文件位于文档根目录中,这也将起作用。

请注意,这无法从子目录(包含 .htaccess 文件)本身“删除”尾部斜杠。为此,您需要使用父目录(即文档根目录)中的.htaccess 文件。

总结

完整的 .htaccess 将变为:

# Disable directory listings (mod_autoindex)
Options -Indexes

# Prevent mod_dir appending trailing slash to directories
DirectorySlash Off

RewriteEngine On

# Remove the trailing slash, should it appear on any 3rd party requests
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteRule /$ /%1 [R=301,L]

# Rewrite the URL to append a trailing slash to directories (internally)
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*[^/])$ $1/ [L]

请注意,这里没有 RewriteBase 指令。

# Remove "/" to "/dashboard"
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule (.*) $1%1/ [L]

This doesn't "remove" anything. In fact, it will append a trailing slash to the end of the URL-path and query string, which seems a bit random?

However, you can't simply remove the trailing slash that occurs after a physical directory in the URL-path, since mod_dir will try to append it with a 301 redirect in order to "fix" the URL.

You can prevent mod_dir from appending the trailing slash with the DirectorySlash Off directive. However, you then need to manually append the trailing slash to the directory with an internal rewrite in order to correctly serve the "index file" (ie. the DirectoryIndex document).

I'm assuming you are linking to the directory without a trailing slash in your internal links.

Try the following instead in the root .htaccess file:

# Disable directory listings (mod_autoindex)
Options -Indexes

# Prevent mod_dir appending trailing slash to directories
DirectorySlash Off

RewriteEngine On

# Rewrite the URL to append a trailing slash to directories (internally)
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*[^/])$ $1/ [L]

A request for /dashboard (no trailing slash) that maps to a physical directory will be internally rewritten to /dashboard/, which will allow the "index file" to be served (by mod_dir also).

For security reasons, you need to ensure that directory listings (mod_autoindex) are disabled, otherwise, directory listings could potentially be generated for directories even when they contain a directory index document. See the security warning in the Apache docs under the DirectorySlash directive.

You need to ensure that your browser cache is cleared before testing since the 301 (permanent) redirect by mod_dir (to append the trailing slash) will have certainly been cached by the browser.

Remove the trailing slash (optional)

You could implement a canonical redirect to actually "remove" the trailing slash from the URL, should there be any requests from third parties (or search engines) that include the trailing slash. (It should already be removed on all your internal links, so this is not required to make your site "work", however, it could be required for SEO to avoid potential duplicate content.)

I'm assuming you don't want the trailing slash on any URL.

You should add the following "redirect" before the rewrite above, immediately after the RewriteEngine directive.

# Remove the trailing slash, should it appear on any 3rd party requests
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.+)/$ /$1 [R=301,L]

The check against the REDIRECT_STATUS environment variable is to ensure we do not redirect the already written request (that appends the trailing slash) by the later rewrite, during the second pass of the rewrite engine. Alternatively, you could use the END flag (Apache 2.4) on the later rewrite.


UPDATE:

.htaccess file not in the root directory

The above assumes the .htaccess file is located in the document root (and therefore applies to all directories and subdirectories thereof). If, however, the .htaccess file is in a subdirectory then you will need to modify the external redirect (that "removes" the trailing slash), since the RewriteRule pattern matches the URL-path relative to the directory that contains the .htaccess file, not the root directory.

So, if the .htaccess file is located in a subdirectory then use the following "redirect" instead:

# Remove the trailing slash, should it appear on any 3rd party requests
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteRule /$ /%1 [R=301,L]

This requires an additional condition (RewriteCond directive) to capture the relevant part of the URL-path from the REQUEST_URI server variable (that contains the full root-relative URL-path), instead of the RewriteRule pattern, which will omit the parent directory(s). The %1 backreference (as opposed to $1) references the captured subpattern from the preceding condition (RewriteCond directive), as opposed to the RewriteRule pattern.

Incidentally, this will also work if the .htaccess file is in the document root.

Note that this is unable to "remove" the trailing slash from the subdirectory (that contains the .htaccess file) itself. For that you would need to make use of the .htaccess file in the parent directory (ie. document root).

Summary

The complete .htaccess would then become:

# Disable directory listings (mod_autoindex)
Options -Indexes

# Prevent mod_dir appending trailing slash to directories
DirectorySlash Off

RewriteEngine On

# Remove the trailing slash, should it appear on any 3rd party requests
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteRule /$ /%1 [R=301,L]

# Rewrite the URL to append a trailing slash to directories (internally)
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*[^/])$ $1/ [L]

Note there is no RewriteBase directive here.

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