htaccess:删除和禁用文件结尾 [英] htaccess: Remove and disable file ending

查看:25
本文介绍了htaccess:删除和禁用文件结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Internet 上到处都说 .htaccess 文件中的这段代码将删除文件结尾:

I'd plastered all over the Internet that this code in your .htaccess file will remove the file ending:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

然而,虽然启用了无文件结尾的 URL,但它并没有禁用带文件结尾的 URL.

However, although enables the URL without file ending, it does not disable the URL with file ending.

本质上,我想强制任何以文件结尾 (domain.com/sample.php) 请求的尝试都以没有文件结尾 (domain.com/sample) 结束

Essentially, what I'd like to force any attempt for an request with file ending (domain.com/sample.php) to end up without file ending (domain.com/sample)

我相信有人会在某处报道过这个,但我找不到任何东西.

I'm sure someone will have covered this somewhere, but I cannot find anything.

非常感谢.

推荐答案

在现有规则之前添加此额外规则:

Add this extra rule before your existing rule:

RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

这篇关于htaccess:删除和禁用文件结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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