arrow right icon

Deep Linking in Angular as well as SEO ranking
Reading Time: 2 minutes

Deep Linking in Angular as well as SEO ranking

Deep Linking

Deep linking is the usage  of the URL, which will take to specific page (content) directly without traversing application from home page [1]. It helps in getting indexed so that these links can be easily searchable by search engines like Google, Yahoo.. etc.


Deep Linking in Angular

With ‘#’:
Angular by default supports deep linking using ‘#’.

It has following drawbacks

  • Search engines (SEO) don’t recognise the url after ‘#’, so the page full path does not get indexed.
  • Google analytics also don’t capture the url after ‘#’, so we may not get accurate results.
  • Readability of the url will not be that good.

Examples for deep linking urls:

Removing ‘#’:
We have to do following changes in angular application code to get rid of ‘#’ in url.

  1. Enable html5Mode:
    We have to enable $locationProvider.html5Mode(true).hashPrefix(‘!’) in angular config function. Some browsers don’t support html5Mode, so we need to add hashPrefix(‘!’).

    angular.module('appModule').config(['$localStorageProvider', function($localStorageProvider) {
  2. Loading files: Generally in HTML header we include files as <script src="vendor/angular.js"></script>.  After enabling html5Mode, these files will not be loaded correctly. To fix this we have to set <base href="/"> or <script src="/vendor/angular.js"></script>
    Note: If you mention href value as ‘/’, it will refer to server base url. If you want to point to other domain path for example in local host, it will not work with ‘/’, so we need to mention it as <base href="http://localhost/appFolder/" />
  3. Enabling Redirection: Reload & Refresh will not work after html5Mode is enabled, because browser will check for folders and it leads to  404 Page Not Found error. To get it worked we need to redirect to index.html file, for that we have to add the following in .htaccess file (or in sever configuration)[2]:
    RewriteEngine on
    # Don't rewrite files or directories
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
  4.  # Rewrite everything else to index.html to allow html5 state links
    RewriteRule ^ index.html [L]

    Note: If you are using debug.html for development and index.html for production, then you have to redirect to debug.html in development phase and index.html in production phase.

  5. You need to remove ‘#’s before urls if you use any. For example we use like <a href="#deep/linking">Deep Linking</a> when we use $routeProvider. In such href links we need to remove “#”