Se si desidera rimuovere l'hash (#), è necessario utilizzare la funzione di riscrittura nel server per servire lo index.html
per tutti i percorsi. In caso contrario, riceverai sempre l'errore 404
. Significa che è possibile reindirizzare lo 404
a index.html
.
Apache: aggiungere una regola di riscrittura per il file .htaccess
come mostrato qui:
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule^- [L]
# If the requested resource doesn't exist, use index.html
RewriteRule^/index.html
nginx: uso try_files
, come descritto nel modello di front controller Web Apps, modificato per servire index.html
:
try_files $uri $uri/ /index.html;
IIS: aggiungere una regola di riscrittura a web.config
, simile a quella mostrata qui:
<system .webserver="">
<rewrite>
<rules>
<rule name="Angular Routes" stopprocessing="true">
<match url=".*">
<conditions logicalgrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchtype="IsFile" negate="true">
<add input="{REQUEST_FILENAME}" matchtype="IsDirectory" negate="true">
</add></add></conditions>
<action type="Rewrite" url="/">
</action></match></rule>
</rules>
</rewrite>
</system>
In Tomcat, è possibile aggiungere in web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<error-page>
<error-code>404</error-code>
<location>/</location>
</error-page>
</web-app>
Dopo reindirizzare il percorso web per index.html
, l'HTML5 Storia API verrà utilizzato in Angular JS per controllare il percorso web.Il nostro compito principale qui è lasciare che sia index.html
chiamato nel server.
fonte
2017-03-20 04:18:10
Potresti aiutare con la soluzione di nginx ?? –
È passato un po 'di tempo da quando l'ho fatto l'ultima volta, ma ci sono molti esempi con esempi come questo: https://gist.github.com/zdwolfe/6721115 In aggiunta a quello che mostrano, probabilmente vorrai un luogo per qualcosa come/statico che serve immagini o file javascript. –