/category/fruit/apple说,我有一个有多个路由的角度项目,所以完整的url是http://myserver/category/fruit/apple,通过路由器链接,一切都很好,但是如果我在输入URL时直接打开这个链接,那么就是404,我的后端是带有404的nodejs。而404是有意义的,因为没有像这样配置的路由。
我的问题,在这种情况下我该怎么办?
我想我有,重定向到根http://myserver?path=category-fruit-apple,并从根组件进行动态路由。这是正确的方式吗?
请推荐最好的方法。
发布于 2019-09-19 08:10:41
也有一个只有前端的解决方案(所以,零配置服务器端),即哈希路由.加一行
useHash: true在根路由器模块的配置(然后,urls将类似于http://myserver/#/category/fruit/apple)。
发布于 2019-09-13 06:29:10
因为它只是javascript,所以服务器不知道路由。
您必须在.htaccess中添加这些行
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因此,如果服务器没有找到这个路由,您可以重定向到您的角根和角可以处理这个路由。因此,您也可以将"index.html“替换为您的角根所在的路由。
发布于 2019-09-16 18:56:30
当然,您需要配置您的服务器来路由到正确的内容。
两个例子是apache http服务器或nginx。
以下是两种情况下的一些最小配置示例。
角apache示例 (.htaccess):
<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示例 (nginx.conf):
server {
listen 80;
server_name your.app;
location / {
proxy_pass http://localhost:4200;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_set_header Host $host;
}
}https://stackoverflow.com/questions/57918153
复制相似问题