Rewrite file extension BUT deny direct access to file(重写文件扩展名但拒绝直接访问文件)
问题描述
我正在寻找一种通过 .htaccess 隐藏文件扩展名并拒绝直接访问的方法.让我们考虑以下几点:
I am looking for a way to hide the file extension via .htaccess and deny direct access. Let's consider the following:
http://www.xyz.zyx/index.php
转换为
http://www.xyz.zyx/index OR http://www.xyz.zyx/
到目前为止一切都很好.我接下来要做的是在用户尝试直接访问时阻止或重定向.例如,如果用户在 URL 栏中键入以下内容(扩展名),则阻止或重定向:
All good till now. What I want to do next is block or redirect when the user tries a direct access. Example, if the user types in the URL bar the following (extension), block or redirect:
http://www.xyz.zyx/index.php
我检查了其他问题的其他答案,但似乎并非如此.
I checked the other answers from other questions, but non seemed to be exactly it.
预先感谢您帮助像我这样的新手.
推荐答案
虽然人们可能会质疑这样一个东西的用处,但它是可行的,所以这里是如何在 .htaccess
中使用 <代码>mod_rewrite:
Although one may question the usefulness of such a thing, it's feasible, so here's how to do it in a .htaccess
using mod_rewrite
:
RewriteEngine On
# Sets your index script
RewriteRule ^$ index.php [L]
# Condition prevents redirect loops (when script is not found)
RewriteCond %{ENV:REDIRECT_STATUS} !^$
RewriteCond %{REQUEST_FILENAME} !-f
# Stop here if the file is not found after a redirect
RewriteRule ^(.*)$ notfound.php [L]
# Condition prevents redirect loops (when script is found)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# Forbid access directly to PHP files
RewriteRule ^.*?.php$ forbidden [F,L]
# Make sure the filename does not actually exist (images, etc.)
RewriteCond %{REQUEST_FILENAME} !-f
# Append the .php extension to the URI
RewriteRule ^(.*)$ $1.php [L]
这篇关于重写文件扩展名但拒绝直接访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:重写文件扩展名但拒绝直接访问文件
基础教程推荐
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01