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?
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With