php – Htaccess not redirecting to 404 error page

Question:

I have a .htaccess with the following configuration:

ErrorDocument 404 /erro404.html
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^index/?$ index.php [NC,L]
        RewriteRule ^index/([a-z0-9-]+)?$ index.php?pagina=$1 [NC]

</IfModule>

But it's not redirecting when I type a page that doesn't exist. What can it be?

Answer:

I always recommend using RewriteBase , this avoids some problems, if index.php is in the root folder:

ErrorDocument 404 /erro404.html

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(index|index/)$ index.php [NC,L]
    RewriteRule ^index/([a-z0-9-]+)$ index.php?pagina=$1 [NC]
</IfModule>

You can also use PATH_INFO instead of GET to get the route:

ErrorDocument 404 /erro404.html

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(index|index/)$ index.php [NC,L]
    RewriteRule ^index/([a-z0-9-]+)$ index.php/$1 [NC]
</IfModule>

In PHP instead of using $_GET['pagina'] , use:

<?php
$path = empty($_SERVER['PATH_INFO']) ? NULL : $_SERVER['PATH_INFO'];

var_dump($path);
Scroll to Top