htaccess的重定向角路线 [英] htaccess redirect for Angular routes

查看:252
本文介绍了htaccess的重定向角路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个角度的应用程序有几个途径,如:

I have an angular application with several routes, such as:

site.com/
site.com/page
site.com/page/4

采用了棱角分明的HTML5路由模式下,这些解决正确的,当你在应用程序中单击链接到他们,但当然是404错误,当你做一个硬刷新。为了解决这个问题,我已经尝试实现基本htaccess的重写。

Using angular's html5 routing mode, these resolve correctly when you click links to them from within the application, but of course are 404 errors when you do a hard refresh. To fix this, I've tried implementing a basic htaccess rewrite.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_METHOD} !OPTIONS
RewriteRule ^(.*)$ index.html [L]

本作品为角度的请求,但是当我尝试加载脚本或者让我的域内Ajax调用,如:

This works for the angular requests, however when I try to load scripts or make ajax calls within my domain, such as:

<script src="/app/programs/script.js"></script>

该脚本不加载 - 它的请求重定向并尝试加载index.html页面的的.htaccess认为它应该重新路由请求 - 不知道这个文件确实存在,它应该加载该文件,而不是重定向。

This script doesn't load - it's request is redirected and it tries to load the index.html page as the .htaccess thinks it should reroute the request - not knowing that this file does exist and it should load the file instead of redirect.

有没有什么方法可以让我有htaccess的请求重定向到的index.html(与视图参数)仅如果没有一个实际的文件,它应该下定决心?

Is there any way I can have the htaccess redirect the request to index.html (with the view parameters) only if there is not an actual file that it should resolve to?

推荐答案

使用一个片段,如:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]

    RewriteRule ^(.*) /index.html [NC,L]

此将跳到实际的资源,如果有一个,并index.html的所有angularjs路由。

This will skip to the actual resource if there is one and to index.html for all angularjs routes.

这篇关于htaccess的重定向角路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆