Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Elastic Beanstalk:从SPA中删除hashbang URL_Angularjs_Apache_Amazon Web Services_Amazon Elastic Beanstalk - Fatal编程技术网

Angularjs Elastic Beanstalk:从SPA中删除hashbang URL

Angularjs Elastic Beanstalk:从SPA中删除hashbang URL,angularjs,apache,amazon-web-services,amazon-elastic-beanstalk,Angularjs,Apache,Amazon Web Services,Amazon Elastic Beanstalk,我有一个使用Elastic Beanstalk托管的AngularJS应用程序,我想从URL中删除hashbang(#!),但在使用配置文件对Apache服务器进行必要修改时遇到了问题 我已经在angular应用程序中启用了HTML5模式,目前我的.ebextensions目录中有以下配置文件 files: "/etc/httpd/conf.d/wsgirewrite.conf": mode: "000644" owner: root group: root

我有一个使用Elastic Beanstalk托管的AngularJS应用程序,我想从URL中删除hashbang(#!),但在使用配置文件对Apache服务器进行必要修改时遇到了问题

我已经在angular应用程序中启用了HTML5模式,目前我的.ebextensions目录中有以下配置文件

files:
  "/etc/httpd/conf.d/wsgirewrite.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
        RewriteEngine On  
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]  
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d  
        RewriteRule ^ - [L]

        RewriteRule ^ /index.html  
没有hashbang,一切都可以正常工作,直到我重新加载页面,得到404,表明重写规则不起作用


如果可能的话,我希望避免任何涉及ssh、复制和修改其中一个默认配置的解决方案,因为如果AWS在将来更改任何默认配置,这可能会使维护成为一场噩梦。Hashbang是不支持HTML5的旧浏览器的回退机制。检查插图:

您可能正在寻找的是如何在AngularJS中配置“漂亮的URL”。除了启用HTML5模式(您似乎已经启用了:
$locationProvider.html5Mode(true)
),您还需要在
中配置
标记,为文档中的所有相关URL指定基本URL/目标

注意:对于不支持HTML5历史API的浏览器,$location服务将自动回退到hashbang方法

根据Angular的文档,如果没有#,url看起来会更好,但它也需要服务器端重写。这是他们为Apache服务器提供的
httpd.conf
示例:

<VirtualHost *:80>
    ServerName my-app

    DocumentRoot /path/to/app

    <Directory /path/to/app>
        RewriteEngine on

        # Don't rewrite files or directories
        RewriteCond %{REQUEST_FILENAME} -f [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^ - [L]

        # Rewrite everything else to index.html to allow html5 state links
        RewriteRule ^ index.html [L]
    </Directory>
</VirtualHost>

ServerName我的应用程序
DocumentRoot/path/to/app
重新启动发动机
#不要重写文件或目录
RewriteCond%{REQUEST_FILENAME}-f[或]
RewriteCond%{REQUEST_FILENAME}-d
重写规则^-[L]
#将所有其他内容重写为index.html,以允许html5状态链接
重写规则^index.html[L]
由于您使用的是Beanstalk,因此需要通过配置
httpd.conf
。Apache有两个可用选项:

  • 要完全覆盖Elastic Beanstalk默认Apache配置,请在
    .ebextensions/httpd/conf/httpd.conf
    的源代码包中包含一个配置
  • 要扩展Elastic Beanstalk默认Apache配置,请将.conf配置文件添加到应用程序源包中名为
    .ebextensions/httpd/conf.d
    的文件夹中(例如
    .ebextensions/httpd/conf.d/port5000.conf
我会推荐扩展选项

参考资料:


您是如何部署应用程序的?eb cli?或者通过控制台?使用eb-Clib部署,并且您已经更改了本地开发环境的配置以删除HasTag?不,我也不知道怎么做。我在本地使用Python Flask的开发服务器,因此修改它将不同于修改prod apache服务器,让它在prod中工作是当务之急,所以还没有投入时间问题是没有从URL中删除hashbang-我可以做得很好,因为我已经启用了html5模式并设置了基本href-问题是我尝试刷新页面时得到的404。请看:没有运气,我按照扩展路径在.ebextensions下创建了httpd/conf.d/目录。没有#的情况下,一切都很好!但是当我重新加载时,我得到的是相同的404。你确定你在使用Apache吗?你用什么语言写代码?PHP?pythonJAVA建议因服务器而异:另外,请确保共享找到的所有日志。下面是如何从Beanstalk的EC2请求日志:我正在使用Apache-我在Python2.7环境中使用Flask应用程序,这意味着根据这些文档,服务器是Apache2.4.33,带有mod_wsgi 3.5。