Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other Redirect non existing files adds history state (.htaccess)

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #154687
    Rugg
    Participant

    I’m using the snippet (below) in the .htaccess to redirect non existing file requests to the domain root. If a non existing file is requested in the url… instead of displaying a 404 Not Found page, the request is directed back to the root…in my case the index.html file.

    For the most part this executes as intended, however, I noticed that an extra history state is added after a redirect. From what I understand, the extra history state is added because a new request is made when directing back to the index.
    This can be problematic when dealing with dynamically loaded content and/or cache configurations.

    With that said, I’m wondering if anyone can suggest a way to prevent the additional history when redirecting…to the index, from the index. Any help is appreciated. Thanks.

    History after default 404:

    → website.com/index
    → website.com/404 ( file not found)

    History after Redirect:

    → website.com/index
    → website.com/index (file not found)

    htaccess sample:

    Options -Indexes -MultiViews +FollowSymlinks
    
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*) / [R=301,L]
    
    #154692
    __
    Participant

    Make it an internal redirect.

    RewriteRule ^(.*)$ / [L]
    

    You can’t make it a 301 this way, however. That’s one-or-the-other: you either tell the browser (and make history), or you don’t (and don’t).

    I would suggest not taking this approach, however. If the page is 404, you should show a 404 page. Redirecting to the home page is confusing, and hides the fact that something went wrong.

    #154752
    __
    Participant

    For whatever reason, the internal redirect still displays the request attempt in the history…in addition to the query string in the url. Perhaps this is the default behavior…

    To clarify, are we talking about the browser history? or apache’s request logs?

    If you use an internal redirect (no R flag, no “http:// …” in the rewrite string), then the browser will never even know it was redirected. There is no indication: Apache simply serves a response, and the browser thinks that its request to “http://example.com/non-existent-page” was successful.

    Apache’s “history” (request logs), OTOH, will always show the request and the redirect.

    #154801
    __
    Participant

    That is very odd.

    I cannot reproduce your results —I tried out the rewrite rule, and the redirect occurs as expected without changing the URL or affecting the browser history.

    NOTE: I did have to rewrite the rule a bit, and use /index.html (or whatever) since / is caught in the !-f test. This causes an infinite loop and 500 error, so I assume you don’t have it written this way, but I thought I’d point it out.

    Maybe it’s something in the Apache config? Is this your own server setup, or a webhost? All I can imagine is that they somehow configured Apache to always send hard redirects.

    #154813
    __
    Participant

    Maybe reduce it to just this, and see what happens…?

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .* index.html [L]
    

    Post the rest of your file, if you like.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘Other’ is closed to new topics and replies.