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

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

问题描述

目前,我有我的所有网页的工作没有他们的.php扩展使用mod_rewrite的。如:

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

www.mywebsite.com/noextension

不过,我将如何去了解我的重定向用户到404页,如果他们决定到PHP扩展添加到URL这样?

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]

我已经试过的东西,如本重写规则\的.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).

更新: 对于它踢我的网站主页,而不/指数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]

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

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