Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

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

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

Whereas, this URL should not:

http://www.example.com/contactus.php?name=john+smith&[email protected]

However, the latter URL breaks my site.

Can this be solved with a simple .htaccess directive?

like image 766
James Anderson Jr. Avatar asked Sep 23 '14 14:09

James Anderson Jr.


People also ask

Should I redirect all 404 to homepage?

404s should not always be redirected. 404s should not be redirected globally to the home page. 404s should only be redirected to a category or parent page if that's the most relevant user experience available. It's okay to serve a 404 when the page doesn't exist anymore (crazy, I know).

How do I view 404 errors in WordPress?

Check the results In the left-hand menu of the WordPress Admin Dashboard, go to Appearance -> 404 Error Page. Select the page you have just customized as your 404 page and set it as 404-error page that'll be displayedappear by default, when users land on a broken link: Click Save Changes and that's it.


1 Answers

You can have this rule as your very first rule in your root .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 protected] won't be impacted.

like image 116
anubhava Avatar answered Oct 19 '22 10:10

anubhava