angularjs - 如何在 apache htaccess 中为 angularjs 应用程序重写 url

标签 angularjs apache .htaccess mod-rewrite

我使用的htaccess如下

RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} -U
RewriteRule ^.*$ - [NC]

RewriteCond %{REQUEST_URI} !-U
RewriteRule ^(.*)$ #/$1 [L]

在我的本地主机上,当我只需输入 localhost/时,它会重定向到 localhost/home 并且我的 Angular 应用程序运行完美。但是当我直接输入 url localhost/home 时,它​​会抛出 404。 我需要将其重写为 localhost/#/home

在我的 Angular 应用程序中,在index.html header 中,我添加了$locationProvider.html5mode(true),html5模式正在工作,但唯一的问题是刷新浏览器时...

提前感谢大家。

最佳答案

RewriteEngine   On
RewriteBase     /
RewriteCond     %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
RewriteCond     %{REQUEST_FILENAME} !-f
RewriteCond     %{REQUEST_FILENAME} !-d
RewriteRule     ./index.html [L]

关于angularjs - 如何在 apache htaccess 中为 angularjs 应用程序重写 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35147465/

相关文章:

.htaccess - 重写 AJAX 抓取规则

javascript - 无法获取日期范围选择器内的值

javascript - 如何在中继器中定位并单击 ng-click 按钮

asp.net-mvc - 使用 MVC、AngularJS 和 Entity Framework 编辑表格行

java - 从 Solr 获取数据到 JTable

.htaccess - 强制执行尾部斜杠,并删除放置在子文件夹中的 htaccess 的 index.php

php - 使用 htaccess 的 ssl 上的不安全内容图像和 js

javascript - 为什么对我的指令使用 ng-repeat 会对我的绑定(bind)产生影响?

php - Apache下载php文件而不是执行脚本

python - pip 能够搜索包但安装失败并出现错误