Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular HTML5 mode, router going to otherwise state on page reload

I am trying to remove hash tag from my website. I have achieved it by following code.

$locationProvider.html5Mode(true);

As well as added base URL in index file as well.

My issue is if i am on contct-us state after reloading entire page. i am redirecting to home state which is defined as otherwise state.

Configuration used are -

server -Appache database - mysql

I have added following code in .htaccess for rewriting rule as well -

     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]

Expected result - After reloading from state contact-us any page i want to go to same page.

like image 302
Chetan Avatar asked Sep 24 '16 08:09

Chetan


1 Answers

In order to support reloading of HTML5 mode route URLs, you need to implement server-side URL rewriting to direct non-file requests (that is, requests that aren't explicitly for an existing file) to your index file, typically index.html.

From the documentation

Using this mode requires URL rewriting on server side, basically you have to rewrite all your links to entry point of your application (e.g. index.html).

Your rewrite rule should not have any # in the destination URL. Instead, use this

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]

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

Also, there's no need to set RewriteBase.

like image 165
Phil Avatar answered Nov 06 '22 16:11

Phil