【问题标题】:Angular JS Encoded URL shows 404 PagAngular JS 编码 URL 显示 404 页面
【发布时间】:2017-01-22 07:52:07
【问题描述】:

我已经创建了 AngularJS 应用程序。

使用下面的 URL 可以正常工作:

http://localhost/AngularDemo/about

如果我像下面这样修改上面的 URL,它会将我带到一个 404 页面:

http://localhost/AngularDemo%2Fabout

我对 .htaccess 文件做了一些改动

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

# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^  index.html#! [L]

【问题讨论】:

  • 为什么要在 URL 中使用转义编码?
  • 通过一些像 Linked In 这样的用户代理,我们确实得到了像上面这样的编码 URL,所以如果我在应用程序中使用该 url,它会将我们带到 404 页面。以上网址是一个示例

标签: javascript angularjs .htaccess


【解决方案1】:

这是默认的 Apache 行为。 URL 路径中的 URL 编码斜杠(即%2F)将隐式触发系统生成 404。这被视为“安全功能”。

如果您需要在 URL 中明确允许编码斜杠,那么您可以在服务器配置中启用此功能:

AllowEncodedSlashes On

参考:
https://httpd.apache.org/docs/2.4/mod/core.html#allowencodedslashes

通过一些用户代理,如 Linked In ...

但是,这通常不是问题。 “用户代理”不应该以这种方式对 URL 进行编码。 /AngularDemo%2Fabout 可能是与 /AngularDemo/about 不同的 URL。

【讨论】:

    【解决方案2】:

    我用下面的代码解决了。你可以写在你的 app.js 文件中

     $locationProvider.hashPrefix('');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-16
      • 2018-12-03
      • 2018-10-10
      • 2013-08-13
      • 1970-01-01
      • 2018-05-27
      • 2015-04-20
      • 1970-01-01
      相关资源
      最近更新 更多