Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess redirect to 404 page RewriteRule

Is there a way to enter a RewriteRule in the htaccess file to redirect to a 404 page if a certain folder/url path as been typed or reached?

For example, if I want every user to be redirected to a 404 page if they get to: www.mydomain.com/abc or www.mydomain.com/abc/ or whatever that comes after "abc", even if that folder really exists, I do not want the users to be able to reach it. If they do reach it, I want them to see the 404 error page. * Please note I am not looking to set up a custom 404 error page, I am looking for a way to redirect to the default 404 page.

How can I do it? Is it possible?

RewriteRule ^abc/(*)?$ [R=404,L] 

And how can I do the same thing in php, redirect to a 404 error page? Once again I am not talking about setting a custom 404 page, I am talking about the default 404 page, to simply redirect a user to the 404 error page using php.

like image 587
user977191 Avatar asked Oct 11 '11 23:10

user977191


People also ask

What is RewriteRule in htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.

How do I redirect a 404 page in HTML?

Just open up a text editor, such as Notepad, and name the file “. htaccess”. Step 2: Add the following text to the . htaccess file: “ErrorDocument 404 /404.


2 Answers

Instead of using mod_rewrite, you can do this with a RedirectMatch directive:

RedirectMatch 404 ^/abc/.*$ 
like image 74
Michał Wojciechowski Avatar answered Sep 24 '22 10:09

Michał Wojciechowski


I did this on my website:

RewriteRule ^404/?$ 404.php ErrorDocument 404 http://www.example.com/404/ 

And on the root of my website I placed a 404.php page to customize that page.

like image 31
paulalexandru Avatar answered Sep 25 '22 10:09

paulalexandru