Forum Moderators: phranque

Message Too Old, No Replies

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

redirection via htaccess

         

markcreid

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

5+ Year Member



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

phranque

2:43 pm on Jan 11, 2018 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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

lucy24

9:35 pm on Jan 11, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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”.