htaccess 代码以删除扩展名并添加+强制尾随斜杠? [英] htaccess code to remove extension AND add+force trailing slash?

查看:24
本文介绍了htaccess 代码以删除扩展名并添加+强制尾随斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些 htaccess 代码放在一起,将 example.com/filename.php 变成 example.com/filename/(并强制使用斜杠)- 我尝试了各种方法,但每种方法都不太正确,从子文件夹上的 500 个错误到尾部斜杠的问题等等......

I'm trying to put together some htaccess code that will turn example.com/filename.php into example.com/filename/ (and force the slash) - I've tried varous approaches, but each hasn't worked quite right, from 500 errors on subfolders to issues with the trailing slash, etc...

请帮忙!

推荐答案

试试这个:

RewriteCond %{THE_REQUEST} ^GET /[^?s]+.php
RewriteRule (.*).php$ /$1/ [L,R=301]

RewriteRule (.*)/$ $1.php [L]

第一条规则将 /foo/bar.php 外部的请求重定向到 /foo/bar<代码>/.第二条规则在内部将 /foo/bar/ 的请求重写为 /foo/bar.php.

The first rule redirects requests of /foo/bar.php externally to /foo/bar/. And the second rule rewrites requests of /foo/bar/ internally to /foo/bar.php.

要强制尾随斜杠,试试这个规则:

And to force the trailing slash, try this rule:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]

这篇关于htaccess 代码以删除扩展名并添加+强制尾随斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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