【问题标题】:Refreshing page gives "Page not found"刷新页面给出“找不到页面”
【发布时间】:2012-12-19 13:57:51
【问题描述】:

我有一个应用程序,它使用单个 ng-view 和多个控制器和视图。 如果我浏览根目录,例如:www.domain.com,一切正常。除了如果我按 Ctrl+R(刷新)然后它给我 404 错误页面未找到。例如:在此页面上刷新给我错误。 http://domain.com/item/1/

但如果我使用 hashbang 访问该页面,那么它可以工作。例如:http://domain.com/#!/item/1/

我该如何解决这个问题? 我的 htacess 是空的。我使用的是支持 html5 hashbang 的 chrome。

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    当浏览器调用http://example.com/#!/item/1/时,就是调用http://example.com/的索引页,你的JS通过分析hashtag来决定展示什么内容。

    当浏览器调用http://example.com/item/1/ 时,您的服务器正在尝试提供http://example.com/item/1/ 的索引页,但它找不到该索引页,因此会引发404 错误。

    要实现您想要的,您需要:

    • 创建重写规则以重写指向您的根索引页面的链接
    • 调整您的 JS,使其生成主题标签而不是 URL。如果您使用的是 AngularJS,请使用 $locationProvider.html5Mode(false); 关闭 html5 模式,或者
    • http://example.com/item/1/ 中放置一个重定向到http://example.com/#!/item/1/ 的索引页 - 但请注意,您需要为每个 /prettyPath/ 重复此操作。

    假设您使用的是 Apache 并且您的索引文件是 index.html,请在尝试其他两种解决方案之前尝试将以下内容添加到您的 .htaccess 文件中以创建重写规则。

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)       /index.html/#/$1 
    </IfModule>
    

    如果您使用的是没有服务器端逻辑的纯 AngularJS/Ajax 解决方案,请将 index.php 更改为 index.html(或 index.htm,具体取决于您的根索引文件名)。

    【讨论】:

    • 谢谢 PassKit。我无法创建索引页面,重定向会减慢进程。所以JS技巧听起来更好。如何以及在哪里调整 JS 以生成正确的 url?
    • 你能发布你的 JS 或 jsfiddle 的链接吗?
    • 您的 URL 似乎设置在 controllers.js 中。将redirectTo: '/annonser/1/' 更改为redirectTo: '/#!/item/1',你应该没问题。您还需要调整其他 URL 以避免其他内容出现同样的问题。
    • 啊我现在明白了。谢谢队友:)
    • 这可能不是最好的解决方案。您的脚本是否附带有关如何使用 .htaccess 设置重写规则的说明?您的服务器能够自动重写这些链接。
    【解决方案2】:

    关于此的伟大博客文章在这里... http://ericduran.io/2013/05/31/angular-html5Mode-with-yeoman/

    “这主要是因为您的 AngularJS 路由不是实际的 html 页面。例如,如果您的 Angular 应用程序中有一条到 /create-order 的路由。如果您从应用程序内部链接到该 URL,则该 URL 可以正常工作但如果用户尝试直接访问该页面,服务器将返回 404。”

    【讨论】:

      【解决方案3】:

      这是 .NET 版本的 URL 重写 (IIS)

      <system.webServer>
          <rewrite>
            <!--This directive was not converted because it is not supported by IIS: RewriteBase /.-->
            <rules>
              <rule name="Imported Rule 1" stopProcessing="true">
                <match url="^index\.html" ignoreCase="false" />
                <action type="None" />
              </rule>
              <rule name="Imported Rule 2" stopProcessing="true">
                <match url="." ignoreCase="false" />
                <conditions>
                  <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                  <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                </conditions>
                <action type="Rewrite" url="/index.html" />
              </rule>
            </rules>
          </rewrite>
        </system.webServer>
      

      【讨论】:

      • 我在 IIS 8.5 上获得了 500 分...有什么想法吗?
      【解决方案4】:

      您只需在 .htaccess 中添加以下脚本

      RewriteEngine On 
      Options FollowSymLinks
      
      RewriteBase /
      
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ /#/$1 [L]
      

      【讨论】:

      • 很好的解决方案......非常感谢,我从过去几周开始搜索...... :)
      • 对我来说也没有用。我在 codeigniter 工作是因为
      • 谢谢。对我很有帮助!
      • 这对我有用。但是现在我在其中一个 php 页面中遇到了 40e pg3 禁止错误。
      • @Shri & Athi 你确定 mod_rewrite 已启用吗?在 Debian/Ubuntu 下,使用 sudo a2enmod rewrite。如果它输出“mod rewrite already enabled”,确实,它“不起作用”,但如果输出类似于“Mod rewrite is now enabled”,这可能是您期望的修复。我想需要重新启动apache。 (sudo service apache2 restart)
      【解决方案5】:

      我无法发表评论,但除了使用HTML modebase href="/"sudo a2enmod rewrite,使用.htaccess 重写。我必须AllowOverride All两个 您网站的可用网站 /etc/apache2 apache.conf

      【讨论】:

        【解决方案6】:
            <ifModule mod_rewrite.c>
              RewriteEngine On
              RewriteCond %{REQUEST_FILENAME} !-f
              RewriteCond %{REQUEST_FILENAME} !-d
              RewriteCond %{REQUEST_URI} !index
              RewriteCond %{REQUEST_URI} !.*\.(css  js|html|png)
              RewriteRule (.*) index.html [L]
            </ifModule>
        

        将其用作 .htaccess

        【讨论】:

        • 我做了,但页面仍然是空白的。
        【解决方案7】:

        截至 2018 年 3 月,只需在您的 .htaccess 文件中添加以下这些行。

        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        #RewriteRule ^(.front-end*)$ /front-end [NC,L,QSA]
        RewriteRule ^(.*)$ /index.html [NC,L,QSA]  
        

        希望这对你有帮助。

        【讨论】:

          猜你喜欢
          • 2021-08-15
          • 2021-07-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-24
          • 2020-01-06
          • 1970-01-01
          • 2016-12-20
          相关资源
          最近更新 更多