如果在 URL 中请求 .php 扩展名,则重定向到 404 [英] Redirect to 404 if .php extension is requested in URL

查看:32
本文介绍了如果在 URL 中请求 .php 扩展名,则重定向到 404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用 mod_rewrite 使我所有的页面都可以在没有 .php 扩展名的情况下工作.如:

I currently have all my pages working without their .php extensions by using mod_rewrite. Such as:

www.mywebsite.com/noextension

但是,如果他们决定像这样将 .php 扩展名添加到 url,我将如何将我的用户重定向到 404 页面?

However, how would I go about redirecting my users to a 404 page if they decide to add the .php extension to the url like this?

www.mywebsite.com/noextension.php

到目前为止,我的 htaccess 看起来像这样:

My htaccess looks like this so far:

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^([a-zA-Z0-9]+)(/)?$ $1.php
RewriteRule ^forums/([a-zA-Z0-9]+)(/)?$ forums.php?category=$1 [NC]

我尝试过诸如这样的RewriteRule .php$ - [R=404],但是,我访问的每一页都只有404.我想我可能不得不使用 REQUEST_URI 之类的东西来完成我的要求.

I have tried things such as this RewriteRule .php$ - [R=404], however, it just 404's every single page I go to. I assume I might have to use something like REQUEST_URI to do what I am asking.

你们建议我添加什么?谢谢.

What do you guys suggest I add? Thanks.

推荐答案

试试这些行(放在你已经拥有的上面):

Try these lines (place above those you have already):

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+).php - [F,L]

此规则将阻止(403 错误)对任何 .php 文件的直接访问(但将通过已重写的 URL 路径,当规则进入下一次迭代时).

This rule will block (403 error) direct access to ANY .php file (but will path trough already rewritten URLs, when rule goes to next iteration).

TBH 我不是 100% 确定它会工作(这取决于你的 Apache 配置)......但考虑到你有 v2.2.x 它应该(它在我的 2.2.17 上工作正常).

TBH I'm not 100% sure that it will work (it depends on your Apache config) ... but considering that you have v2.2.x it should (it works fine on mine 2.2.17).

更新:对于它在没有/index 的情况下将我的网站主页踢到 404" 在上述规则之前添加此行:

UPDATE: For "its kicking my web sites homepage without /index to a 404" add this line before the above rule:

RewriteRule ^$ /index.php [QSA,L]

这篇关于如果在 URL 中请求 .php 扩展名,则重定向到 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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