S
S
Snooper2017-09-08 00:14:22
Apache HTTP Server
Snooper, 2017-09-08 00:14:22

Disable 302 redirect if page doesn't exist (404) in Apache?

The essence of the problem:
When you open a link to a non-existent page of the domain.tld/test_page type, the status 404 is immediately given.
When you open a link to a non-existent page of the domain.tld/test_page/ type (slash at the end), a 302 redirect occurs first to the same address without a slash and the status is added 404.
How can you avoid the appearance of status 302 for non-existent pages opened with a slash at the end?
All existing links should continue to redirect from versions with a slash to versions without a slash.
.htaccess content

Options -Indexes
ErrorDocument 404 /404.php

<IfModule mod_php5.c>
        php_flag allow_call_time_pass_reference 1
        php_flag session.use_trans_sid off

        #php_value display_errors 1

        #php_value mbstring.internal_encoding UTF-8
</IfModule>

<IfModule mod_rewrite.c>
ErrorDocument 404 /404.php
        Options +FollowSymLinks
        RewriteEngine On

      #delete www
       RewriteCond %{HTTP_HOST} ^www\.(.*)$
        RewriteRule ^(.*)$ http://%1/$1 [L,R=301]


#RewriteCond %{REQUEST_URI} ^/404/$
#RewriteRule ^(.*)$ /404.php [L]

        #RewriteCond %{REQUEST_URI} ^/404
        #RewriteRule . - [R=404,L]
         
        #redirect from slash
        RewriteCond %{REQUEST_URI} (.+)/$
        RewriteRule ^ %1 [R,L]
        
        RewriteCond %{REQUEST_URI} ^.*[^/]$
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}/index.php -f
        RewriteRule ^(.*)$ /$1/index.php [L]


        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-l
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !/bitrix/urlrewrite.php$
        RewriteRule ^(.*)$ /bitrix/urlrewrite.php [L]
        RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]

</IfModule>

<IfModule mod_dir.c>
        DirectorySlash Off
        DirectoryIndex index.php index.html
</IfModule>

<IfModule mod_expires.c>
        ExpiresActive on
        ExpiresByType image/jpeg "access plus 3 day"
        ExpiresByType image/gif "access plus 3 day"
</IfModule>

404.php content
<?
include_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/urlrewrite.php');

CHTTP::SetStatus("404 Not Found");
@define("ERROR_404","Y");

require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php");

$APPLICATION->SetTitle("404 Not Found");
?>

    <div class="wrap_nofind_search">
        <div class="top_nofind_search">
            <h2>404 Страница не найдена</h2>

            <p>Извините, запрошенная Вами страница не найдена</p>
        </div>

    </div>

<?require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php");

301284cdafb742799b269d025efc3ddb.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Mukovoz, 2017-09-08
@Snooper

Let's reason logically. You have a forwarding from without a slash to a slash, and it will work everywhere. She doesn't care if the page is there or not. But there is still a way out. This redirection can be done not by means of apache, but by means of php, which can check if the page exists or not, and if it does not exist, give 404, and if it exists, give 302)
Of course, I can be wrong and maybe Apache can still do this, but I have no idea how . But I have an idea how nginx can do this. Although I haven't tested it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question