如何为AngularJS应用程序在Apache htaccess中重写URL [英] How to rewrite url in apache htaccess for angularjs app

查看:61
本文介绍了如何为AngularJS应用程序在Apache htaccess中重写URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的htaccess如下

The htaccess I'm using is as follow

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并且我的角度应用程序运行良好.但是,当我直接输入url localhost/home时,它会抛出404. 我需要将其重写为localhost/#/home

On my localhost, when I'm simply typing localhost/ it's redirecting to localhost/home and my angular app is running perfectly. But when I'm directly typing the url localhost/home it's throwing 404. I need to rewrite it as localhost/#/home

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

In my angular app, in index.html header, I've added and $locationProvider.html5mode(true), html5 mode is working, but only problem is when refreshing browser...

预先感谢大家.

推荐答案

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中重写URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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