Welcome to WebmasterWorld Guest from 23.20.245.192

Forum Moderators: Ocean10000 & incrediBILL & phranque

How to redirect everything after “www.mydomain.com/page-url/ ” to 404

redirection via htaccess

     
8:49 am on Jan 11, 2018 (gmt 0)

New User

joined:Jan 11, 2018
posts: 1
votes: 0


Is there a way to redirect to a 404 page if a certain type of URL path as been typed or reached?

I mean, if I want every user to be redirected to a 404 page if they get to:
www.mydomain.com/page-url/anything/
or whatever that comes after
/page-url/
, even if that page exists.

I tried this in .htaccess

RewriteRule %{REQUEST_URI} ^(.*)$ [R=404,L]

But its not worked for me. :(
please help
2:43 pm on Jan 11, 2018 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11238
votes: 126


welcome to WebmasterWorld [webmasterworld.com], markcreid!

you can use the [G] flag for this:
https://httpd.apache.org/docs/current/rewrite/flags.html#flag_g
9:35 pm on Jan 11, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14624
votes: 599


Why on earth would you want to redirect to the 404 page? Fortunately, your code doesn't do that: it returns a 404 response. (Incidentally, the [L] isn't needed, though it will do no harm. Any number outside the 3nn range carries an implied [L] anyway.)

I tried this in .htaccess

RewriteRule %{REQUEST_URI} ^(.*)$ [R=404,L]
And it didn't crash your server? Huh. It looks as if you are mixing up two different syntaxes: the one for a RewriteRule and the one for a RewriteCond. All you need is
RewriteRule ^directory/ - [R=404]
No closing anchor; that's how you ensure that the rule applies to the entire contents of the directory. You might even omit the final slash.

You didn't explain why you want to do this. But sometimes returning a manual 404 is exactly the right thing to do: for example, if malign robots are snuffling around for your WordPress files. A 403 response says “I’m on to you and we don’t want your kind”, while a 404 response simply says “Sorry, can’t find it”. (I do it myself for certain types of request.)

Edit: If you are dealing with URLs that formerly existed but have now been removed, then as phranque says the [G] flag (shorthand for R=410) is even better. Some robots, notably the Googlebot, will go away faster when they meet a 410, because it says “I have intentionally removed this page”.