如何使用 htaccess 在 url 中删除带有最后一个可选尾部斜杠的 txt 扩展名的需要

How to remove the need of txt extension with a final optional trailing slash in url using htaccess

如何在 php

中使用 htaccess 正则表达式删除 txt 文件的 txt 扩展名并在 url 中添加最后一个可选的尾部斜线

我的密码是

   #turn on url rewriting 
RewriteEngine on

#remove the need for .txt extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.txt -f
RewriteRule ^(.*)(/?)$ .txt

但是它抛出 500 服务器 error.What 代码有问题吗?

根据您显示的示例,您能否尝试执行以下操作,请确保在测试您的 URL 之前清除浏览器缓存。公平警告我目前还没有测试它。

RewriteEngine ON
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/ !-d
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{DOCUMENT_ROOT}/\.txt -f
RewriteRule ^(.*)/?$ .txt [L]

编辑: 根据 OP 的评论添加以下解决方案。

RewriteEngine ON
RewriteBase /
RewriteCond %{REQUEST_URI} /$
RewriteRule ^(.*)/$  [R=301,L]

RewriteCond %{DOCUMENT_ROOT}/ !-d
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{DOCUMENT_ROOT}/\.txt -f
RewriteRule ^(.*)/?$ .txt [L]