在Nginx中,如果你想配置301重定向,使得访问一个目录时自动添加斜线(/),你可以使用如下配置: ```nginx server { listen 80; server_name yourdomain.com; location / { if (!-e $request_filename) { rewrite ^/(.*)$ /$1/ permanent; } } # 或者,如果你只想对特定的目录做这个处理,例如 'example' location /example { if ($uri !~* ^/example/) { return 301 $scheme://$host/example/; } # 其他配置... } } ``` 上述配置中,`if (!-e $request_filename)` 检查请求的文件或目录是否存在。如果不存在,那么 `rewrite ^/(.*)$ /$1/ permanent;` 将会重写URL并在末尾添加一个斜线,同时返回301 Moved Permanently状态码。 对于特定目录的处理方式,直接在location块中检查并返回301重定向。 [2024-02-01 16:58:47 | AI写代码神器 | 229点数解答]