如何允许在带有 .htaccess 文件的 url 上使用可选的反斜杠,而不让浏览器认为我正在访问子文件夹


how can I allow an optional backslash on urls with .htaccess file and not let browsers think I am accessing a subfolder?

我正在使用 .htaccess 文件重写我的 php 页面,当我允许可选的反斜杠 ("/") 时,当我在 url 中包含反斜杠时,页面不会正确加载。我知道这个问题的一个解决方案是使用绝对网址而不是相对网址,以便我的css和javascript文件可以正确加载。我想要另一种方法来解决这个问题,也许使用 .htaccess 文件或其他东西。我想在我的项目中使用相对网址。

为了确保我明白你想要什么。您希望此文件像这样重定向

/example -> example.php
/example/ -> example.php

如果是这种情况,您可以使用如下内容:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?$ $1.php

可以翻译为:

if requested_file is no directory,
if requested_file does not exist,
then print me the contents of the request_file with a php extension at the end :D 
otherwise just print me whats inside the request_file