- This topic is empty.
-
AuthorPosts
-
October 30, 2013 at 7:34 pm #154687RuggParticipant
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 theindex.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]
October 30, 2013 at 9:52 pm #154692__ParticipantMake 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.
October 31, 2013 at 10:27 am #154752__ParticipantFor 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.
October 31, 2013 at 2:12 pm #154801__ParticipantThat 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.
October 31, 2013 at 4:11 pm #154813__ParticipantMaybe 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.
-
AuthorPosts
- The forum ‘Other’ is closed to new topics and replies.