如何使用.htaccess从URL中删除扩展名而没有结尾斜杠? [英] How to remove extension from url WITHOUT trailing slash using .htaccess?

查看:51
本文介绍了如何使用.htaccess从URL中删除扩展名而没有结尾斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.htaccess文件:

I have a .htaccess file:

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

我有一个名为random.php的文件.

我只想调用something.com/random,但是Apache在URL的末尾添加了斜杠(使用301重定向),因此出现错误,指出找不到something.com/random/.php.

All I want to do is just call something.com/random, but Apache adds a trailing slash (using 301 Redirect) to the end of the URL, therefore giving an error stating that something.com/random/.php cannot be found.

当我使用

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [NC,L]

然后无法加载我的外部文件(.js,.css等),服务器将以500 Internal Server Error响应进行响应. Apache说重定向太多.

then my external files (.js, .css, etc.) can't load and the server responds with a 500 Internal Server Error response. Apache says that there were too many redirects.

推荐答案

使用可选的斜杠尝试使用此规则:

Try this rule with optional trailing slash:

RewriteEngine On

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

这篇关于如何使用.htaccess从URL中删除扩展名而没有结尾斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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