如何使用 .htaccess 文件缩短我的 url
How to shorten my url using .htaccess file
我的url:
www.example.com/about/about_me.html
我希望它看起来像这样并打开 about_me.html 文件
www.example.com/about
我想搜索我的域名后跟 /about,打开的页面是 about_me.html。我想用我的 htacess 文件来做这件事。另一件事是我将 .htacess 文件放在服务器中的位置
这变得更加复杂,因为 /about
映射到文件系统上的物理目录。通常,mod_dir 将附加尾部斜杠(使用 301 重定向)以“修复”URL。为了防止这种情况,我们需要禁用这种行为,但是,这样做有潜在的警告和安全隐患。如果它被禁用,您可能需要在系统的其他地方手动覆盖它。
在根 .htaccess
文件中尝试以下操作:
# Disable directory listings (mod_autoindex)
Options -Indexes
# Prevent mod_dir appending the trailing slash
DirectorySlash Off
# Enable the rewrite engine (mod_rewrite)
RewriteEngine On
# Internally rewrite "/about" to "/about/about_me.html"
RewriteRule ^about$ about/about_me.html [L]
现在,/about
的请求将服务于 /about/about_me.html
。但是对 /about/
的请求(带有尾部斜杠)将导致 403 Forbidden(假设 /about
子目录中没有 DirectoryIndex
文档)。
并且请求任何其他没有尾部斜杠的目录(例如 /subdirectory
)也会导致 403 禁止访问,无论该目录中是否存在 DiretcoryIndex
文档。
mod_autoindex 需要禁用(即 Options -Indexes
),以防止在设置 DirectorySlash Off
时意外信息泄露。请参阅 DirectorySlash
的相关 Apache 手册页:https://httpd.apache.org/docs/2.4/mod/mod_dir.html#directoryslash
为了让生活更轻松,请避免使用直接映射到文件系统目录的 URLs(没有尾部斜线)。例如,您可以将所有内容存储在 /content
子目录中,然后 /about
URL 映射到 /content/about/about_me.html
。然后您不需要禁用 DirectorySlash
(如果需要,阻止直接访问 /content
是微不足道的)。
例如:
# Enable the rewrite engine (mod_rewrite)
RewriteEngine On
# Internally rewrite "/about" to "/content/about/about_me.html"
RewriteRule ^about$ content/about/about_me.html [L]
我的url:
www.example.com/about/about_me.html
我希望它看起来像这样并打开 about_me.html 文件
www.example.com/about
我想搜索我的域名后跟 /about,打开的页面是 about_me.html。我想用我的 htacess 文件来做这件事。另一件事是我将 .htacess 文件放在服务器中的位置
这变得更加复杂,因为 /about
映射到文件系统上的物理目录。通常,mod_dir 将附加尾部斜杠(使用 301 重定向)以“修复”URL。为了防止这种情况,我们需要禁用这种行为,但是,这样做有潜在的警告和安全隐患。如果它被禁用,您可能需要在系统的其他地方手动覆盖它。
在根 .htaccess
文件中尝试以下操作:
# Disable directory listings (mod_autoindex)
Options -Indexes
# Prevent mod_dir appending the trailing slash
DirectorySlash Off
# Enable the rewrite engine (mod_rewrite)
RewriteEngine On
# Internally rewrite "/about" to "/about/about_me.html"
RewriteRule ^about$ about/about_me.html [L]
现在,/about
的请求将服务于 /about/about_me.html
。但是对 /about/
的请求(带有尾部斜杠)将导致 403 Forbidden(假设 /about
子目录中没有 DirectoryIndex
文档)。
并且请求任何其他没有尾部斜杠的目录(例如 /subdirectory
)也会导致 403 禁止访问,无论该目录中是否存在 DiretcoryIndex
文档。
mod_autoindex 需要禁用(即 Options -Indexes
),以防止在设置 DirectorySlash Off
时意外信息泄露。请参阅 DirectorySlash
的相关 Apache 手册页:https://httpd.apache.org/docs/2.4/mod/mod_dir.html#directoryslash
为了让生活更轻松,请避免使用直接映射到文件系统目录的 URLs(没有尾部斜线)。例如,您可以将所有内容存储在 /content
子目录中,然后 /about
URL 映射到 /content/about/about_me.html
。然后您不需要禁用 DirectorySlash
(如果需要,阻止直接访问 /content
是微不足道的)。
例如:
# Enable the rewrite engine (mod_rewrite)
RewriteEngine On
# Internally rewrite "/about" to "/content/about/about_me.html"
RewriteRule ^about$ content/about/about_me.html [L]