angularjs未应用Apache重写规则

angularjs未应用Apache重写规则,apache,mod-rewrite,rewrite,Apache,Mod Rewrite,Rewrite,我正在尝试使用AngularJS设置Apache2.2,其结构与下面封闭式问题中的结构几乎完全相同 我想将所有请求重定向到app/index.html,但对/api的请求除外 我的目录结构如下 / /应用程序 /app/index.html /应用程序/css /app/js /原料药 我试图在httpd vhosts文件中应用这些重定向规则,该文件被发布为问题的正确答案 <VirtualHost *:80> ServerName www.test.com DocumentRo

我正在尝试使用AngularJS设置Apache2.2,其结构与下面封闭式问题中的结构几乎完全相同

我想将所有请求重定向到app/index.html,但对/api的请求除外

我的目录结构如下

  • /
  • /应用程序
  • /app/index.html
  • /应用程序/css
  • /app/js
  • /原料药
我试图在httpd vhosts文件中应用这些重定向规则,该文件被发布为问题的正确答案

<VirtualHost *:80>
ServerName www.test.com
DocumentRoot "C:/websites/test"
Alias /CFIDE "C:/websites/CFIDE"

RewriteEngine On

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/api

# otherwise forward it to index.html 
RewriteRule ^.*$ - [NC,L]
RewriteRule ^app/. /app/index.html [NC,L]

</VirtualHost>

服务器名www.test.com
DocumentRoot“C:/websites/test”
别名/CFIDE“C:/websites/CFIDE”
重新启动发动机
#如果存在目录或文件,请直接使用它
RewriteCond%{REQUEST_FILENAME}-s[或]
RewriteCond%{REQUEST_FILENAME}-l[或]
RewriteCond%{REQUEST_FILENAME}-d
重写cond%{REQUEST_URI}/应用程序编程接口
#否则将其转发到index.html
重写规则^.*$-[NC,L]
重写规则^app//app/index.html[NC,L]
但是,当我尝试访问/在浏览器中时,我会看到一个空白屏幕。但是,如果我在浏览器中输入/app/index.html,我可以在浏览器中查看html页面,但无法访问css或js,并且返回404

我一直在为这件事发狂,这似乎是一件简单的事情来完成apache重写


任何帮助都将不胜感激

我也有同样的问题。但是我对修改代码一无所知,所以我不得不用谷歌搜索很多东西。 我在这封电子邮件中找到了解决方案:

因此,我认为您的.htaccess应该如下所示:

Options +FollowSymLinks
IndexIgnore */*

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/api
RewriteRule ^.*$ - [NC,L]

# otherwise forward it to index.html 
RewriteRule ^app/(.*) /app/#/$1 [NC,L]
注意
(.*)
#/$1

注意:您必须在包含CSS、JS等的文件中使用绝对路径。否则,您将得到错误:

解释为脚本但使用mime类型text/html传输的资源


在Apache2.2.16+中,使用FallbackResource指令可以更轻松地实现这一点

FallbackResource /app/index.html

根据您转发对API的访问的方式,您可能还需要利用机柜来禁用专门针对API请求(2.2.24+)的回退资源


后备资源已禁用
对我有用:

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !index
    RewriteRule (.*) index.html [L]
</ifModule>

重新启动发动机
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写cond%{REQUEST_URI}!指数
重写规则(.*)index.html[L]

不知道您是否仍然存在此问题,但我在AngularJS和Apache2上也有相同的症状

我的问题实际上是我在使用IE,它会自动进入兼容模式。我在我的HTML中使用了下面这行代码,它起了作用

<meta http-equiv="X-UA-Compatible" content="IE=edge" />


此处进一步描述:<>。

我已将这些规则应用于httpd vhosts文件,但当我尝试访问
mywebsite.com
时,它不会传递到index.html文件,现在当我访问
mywebsite.com/app/
时,我在浏览器中看到一个404。我使用了您的代码,并看到“内部服务器错误”消息。关于如何解决这个问题,你有什么想法吗?只是想补充一下,如果你的Angular应用程序不在主机的根目录下,也就是说,内容位于类似的目录中,你需要添加相应的
标记。例如,您的标记需要是
-这就是您的资产和模板的加载方式correctly@BahamutWC:不鼓励使用基本标签@Daan只在Angular建议您从根文档提供时才建议使用它。但是,如果出于任何原因不能使用,则必须使用
标记。我一直使用此方法收到错误的请求。修复它的方法是在index.html前面加一个斜杠,比如说
RewriteRule(.*)/index.html[L]
这个对我来说非常合适,因为前端(angular)是index.html,子文件夹/是后端的laravel index.php,所以定义了索引。
html
让它对我来说很有用。在apache 2.4.x中非常有用!
<meta http-equiv="X-UA-Compatible" content="IE=edge" />