Rohit Jindal Rohit Jindal - 3 months ago 8
AngularJS Question

TypeError : Cannot read property 'replace' of undefined

Overview :

I would like to redirect the visitor on a default page(index.html) inside the directory structure to display when a directory is accessed using

.htaccess
file(as of now current directory index.html is loading).

But When I turn on
$locationProvider.html5Mode(true)
in my angular application getting below error.

Console Error :

enter image description here

Directory structure of Application :

public_html
--prod
--public
--app
--controllers
--directives
--services
--app.config.js
--index.html
--.htaccess
.htaccess


public_html
=>
.htaccess :

DirectoryIndex prod/public/index.html


This
.htaccess
is used to redirect the visitors on a application page (
prod/public/index.html
) directly by using domain name, as of now when user visit the domain the current directory(
public_html
) index.html is loading.

public_html
=>
prod
=>
.htaccess :

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /prod/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /prod/public/#/$1
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/prod/$1 [L,R=301]
</IfModule>


prod
=>
public
=>
index.html :

<base href="/prod/public/">


Everything working fine If i removed
$locationProvider.html5Mode(true)
from my
app.config.js
file.So, when I comment out the
$locationProvider
code, everything works fine in
#
mode. I can get to
/#/abc
, etc. without issue. As soon as I put the
$locationProvider
parts back in, nothing happen.

Related issues on Stack Overflow with no success :

$locationProvider.html5Mode(true) issues

Angular html5 mode not working in apache

Answer

Inside public_html => .htaccess have this rule instead:

DirectoryIndex index.html
RewriteEngine On

RewriteRule ^/?$ prod/public/index.html [L]

then public_html => prod => .htaccess should have this:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ public/index.html [L]
Comments