HTML5 history pushState link back

After viewing another post earlier in the HTML5 History API and reading a tutorial in Mozilla , I was able to implement a basic working demo to use this API to allow the URL to “rewrite” without reloading the page and without using a hash.

My question is: Let's say you have a user who comes to the page, clicks on one of these links, which uses the history API to write a new URL. Then the user enters the page on the page. Now I assume that there will now be bookmarks of the rewritten URL. So, when the user returns in a couple of days or something else and tries to go to the bookmarked page, will he return 404? So how can you somehow implement a way to solve it?

The URL I'm rewriting is supposed to point to a nonexistent location.

+3
source share
3 answers

, , pushstate. , , , artist.php. , . , "" , artist.php? Artist = travel

, , , ajax (artist.php? artist = travel & ajax = true). "" artist.php? Artist = , , , .

, - URL-, -.

+1

. URL- .

0

: pushstate HTML5, , , 404.

jst pushstate , , , JS .

The easiest solution is to add a rewrite rule to your Nginx or Apache server to internally rewrite all calls to the same index.html page. The browser believes that it is given a unique page when it is actually the same page:

Apache (in your vhost if you use it):

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
 </IfModule>

Nginx

rewrite ^(.+)$ /index.html last;

You would of course add some JS logic to display the correct content. In my case, I use Backbone routes that easily handle this.

0
source

Source: https://habr.com/ru/post/1791785/


All Articles