Htaccess is not redirecting to 404 error page

Asked

Viewed 2,341 times

4

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 he’s not redirecting when I type in a page that doesn’t exist. What can it be?

2 answers

1

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);

0

Try putting these commands on top of .htaccess

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d

Browser other questions tagged

You are not signed in. Login or sign up in order to post.