.htaccess重写可选的尾部斜杠 [英] .htaccess rewrite optional trailing slash

查看:63
本文介绍了.htaccess重写可选的尾部斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用.htaccess中的以下代码来排除.html之外的网站:

I currently have my website excluding the .html using the following code in .htaccess:

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

这很好用,但是如果我添加一个斜杠,我会得到404.

And this works great, but if I add a trailing slash I get a 404.

我希望可以选择现在使用,也可以在末尾添加斜杠.

I'd like to have the option of having it work as it is now and also if I add a trailing slash at the end.

这可能吗?

谢谢

推荐答案

可以使用:

Options -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]

这篇关于.htaccess重写可选的尾部斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆