如果用户在页面名后面输入一个“/”(或在“/”之后的任何内容),我如何将用户重定向到一个错误404页面?

时间:2022-10-07 08:01:45

On an Apache/Linux Server, how do I redirect users to an error 404 page if they enter a trailing "/" (or anything after a "/") after the page name?

在Apache/Linux服务器上,如果用户在页面名后面输入一个“/”(或在“/”后面输入任何内容),那么如何将用户重定向到错误404页面?

For example, this URL should direct to an error page:

例如,这个URL应该指向一个错误页面:

http://www.example.com/contactus.php/someextrastuffhere

Whereas, this URL should not:

鉴于此URL不应:

http://www.example.com/contactus.php?name=john+smith&email=johnsmith@webpage.com

However, the latter URL breaks my site.

然而,后一个URL破坏了我的站点。

Can this be solved with a simple .htaccess directive?

用一个简单的。htaccess指令可以解决这个问题吗?

1 个解决方案

#1


3  

You can have this rule as your very first rule in your root .htaccess:

在根.htaccess中,可以将此规则作为您的第一个规则:

RewriteRule ^.+?\.(php|html?)/ - [L,R=404,NC]

This will send any /file.php/foo request to 404 whereas http://www.example.com/contactus.php?name=john+smith&email=johnsmith@webpage.com won't be impacted.

这将发送任何/文件。php/foo请求404,而http://www.example.com/contactus.php?name=john+smith&email=johnsmith@webpage.com不会受到影响。

#1


3  

You can have this rule as your very first rule in your root .htaccess:

在根.htaccess中,可以将此规则作为您的第一个规则:

RewriteRule ^.+?\.(php|html?)/ - [L,R=404,NC]

This will send any /file.php/foo request to 404 whereas http://www.example.com/contactus.php?name=john+smith&email=johnsmith@webpage.com won't be impacted.

这将发送任何/文件。php/foo请求404,而http://www.example.com/contactus.php?name=john+smith&email=johnsmith@webpage.com不会受到影响。