【问题标题】:Angular HTML5 mode, router going to otherwise state on page reloadAngular HTML5 模式,路由器将在页面重新加载时处于其他状态
【发布时间】:2017-02-02 02:41:17
【问题描述】:

我正在尝试从我的网站中删除井号标签。我通过以下代码实现了它。

$locationProvider.html5Mode(true);

以及在索引文件中添加的基本 URL。

我的问题是如果我在重新加载整个页面后处于联系我们状态。我正在重定向到定义为其他状态的家乡状态。

使用的配置是 -

服务器-Appache 数据库——mysql

我还在 .htaccess 中添加了以下代码用于重写规则 -

     RewriteEngine On
     php_value post_max_size 120M 
    php_value upload_max_filesize 120M 
     php_value max_execution_time 90 



     RewriteCond %{HTTP_HOST} ^54\.201\.153\.244$ [NC,OR]

     RewriteCond %{HTTPS} off
     RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

      RewriteCond %{HTTP_HOST} ^adkarlo.com$ [NC]
        RewriteRule ^(.*)$ https://www.adkarlo.com/$1 [R=301,L]

      #RewriteBase /html/
      ErrorDocument 404 /404.php

    RewriteCond %{REQUEST_FILENAME} -f
     RewriteRule ^ - [L]
   RewriteRule ^(.*)$ index.php [L]

预期结果 - 从状态联系我们重新加载后我想转到同一页面的任何页面。

【问题讨论】:

  • 路径中不应有任何#/
  • @charlietfl - url 中没有任何 #/ 但有一次,我从 /about 刷新页面。它正在将我重定向到 /home 状态,这是我在 stateProvider 中以其他条件给出的状态。
  • @charlietfl - 如果您建议我任何解决方案,我将非常感激。 :)
  • 他的意思是你的RewriteRule应该指向index.html。见github.com/angular-ui/ui-router/wiki/…
  • 对不起@Phil 我听不懂。会试试这个,让你知道。

标签: php angularjs .htaccess


【解决方案1】:

为了支持 HTML5 模式路由 URL 的重新加载,您需要实现服务器端 URL 重写以将非文件请求(即未明确针对现有文件的请求)定向到索引文件,通常index.html.

来自documentation

使用此模式需要在服务器端重写 URL,基本上您必须重写所有指向应用程序入口点的链接(例如 index.html)。

您的重写规则不应在目标 URL 中包含任何 #。相反,使用这个

RewriteEngine on

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

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

来源~https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode

另外,不需要设置RewriteBase

【讨论】:

  • 我已经编辑了 .htaccess 文件中的问题和代码仍然遇到同样的问题。请帮帮我
  • @Chetan 为什么你有index.php?/?它应该只是index.php
  • @Chetan 请更新您的问题以准确反映您的代码/配置的当前状态
  • 已编辑问题...仍然面临同样的问题。
  • @Chetan 可能是一个愚蠢的问题,但您是否启用了mod_rewrite 模块?可以分享一下你的路由配置吗?
猜你喜欢
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
  • 2016-04-10
  • 1970-01-01
  • 2021-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多