【问题标题】:Url Not found on reload + Angular5重新加载时找不到网址+ Angular5
【发布时间】:2018-03-31 06:07:41
【问题描述】:

我面临一个与角度应用程序相关的问题,在我的应用程序中使用组件,然后当我使用单个页面时,页面加载成功,但是当我在新选项卡中打开该 URL 或重新加载它时,给出错误未找到。

我为此使用 AWS apache 服务器,并且只在其上遇到此问题,在我的 Godaddy 服务器页面重新加载正常工作。

这里附上我的错误截图。[

提前致谢

【问题讨论】:

  • 您可以从stackoverflow.com/questions/47373927/… 找到解决方案。刷新后,您的服务器无法解析 url,因为它是由 angular 而不是服务器生成的
  • 感谢您的快速回复,您能否详细说明此代码将应用于哪个文件?
  • 我认为您需要检查config 文件中的apache server。您可能需要重定向某些 url 模式。我建议你用谷歌搜索 AWS apache 服务器并用你的发现更新你的问题。我会尝试从那里获取它,因为我也不太了解这个 conf 文件。
  • @ShashankVivek 谢谢,我已经尝试过了,但没有找到解决方案。

标签: apache amazon-ec2 angular5


【解决方案1】:

我认为您的 .htaccess 文件无法访问,

在 /var/www/html 中创建 .htaccess 文件,即在 index.html 所在的文件夹中。

将以下代码放入其中:-

RewriteEngine On
    # If an existing asset or directory is requested go to it as it is
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
    RewriteRule ^ - [L]
    # If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html 

转到 /etc/apache2 文件夹并使用 sudo 打开 apache2.conf 文件。

cd /etc/apache2
sudo vim apache2.conf

删除 AccessFileName 的注释(#),它看起来像

AccessFileName .htaccess 

然后找到有的那一行

<Directory /var/www/>
     Options Indexes FollowSymLinks
     AllowOverride None
     Require all granted
</Directory>

将“无”替换为“全部”

AllowOverride All

完成!

现在运行命令

sudo service apache2 restart

现在检查!

【讨论】:

  • 就我而言,添加如上所示的 .htaccess 文件足以防止网站崩溃。所以可能不需要直接的服务器配置(当然,如果服务器是预先配置的)。
【解决方案2】:

在您的项目根文件夹中添加 .htaccess 文件。 在您的 Appache .htaccess 文件中添加以下代码:

RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

Nginx .conf 文件:

location / {
  if (!-e $request_filename){
    rewrite ^(.*)$ /index.html break;
  }
}

执行命令:

sudo a2enmod rewrite

sudo service apache2 restart

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    相关资源
    最近更新 更多