Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a SEO friendly Url Slug in Dynamic Views of Angular UI-Router

In an Angular setting, I have chose Angular UI-router to switch between views.

My config looks as follows:

.config(function($stateProvider, $urlRouterProvider) {

  $urlRouterProvider.otherwise('/app/home');

  $stateProvider

    // Nav
    .state('app', {
      url: '/app',
      templateUrl: 'templates/navbar.html',
      abstract: true,
      controller:'AppCtrl as app',
    })

    // Home
    .state('app.home', {
      url: '/home',
      templateUrl: 'templates/home.html',
      controller:'HomeCtrl as home',
    })

    .state('app.browse', {
      url: '/browse',
      templateUrl: 'templates/browse.html',
      controller:'BrowseCtrl as browse',
    })

    .state('app.browse-detail', {
      url: '/browse/:productId',
      templateUrl: 'templates/browse-detail.html',
      controller:'BrowseDetailCtrl as detail',
    })

})

This will result that the url of a product will appear as follows:

www.website.com/app/#/browse/productId

Instead I would like to see something like:

www.website.com/browse/productId/most-awesome-product

where most-awesome-product is an Url Slug.

My questions are:

  • what are in general the principles of making Angular Websites SEO friendly using Routing?
  • how can I change the url of my angular router with adding the url slug (see above)?
  • will changing the url make my website SEO friendly?

Thanks!

like image 989
JohnAndrews Avatar asked Sep 07 '15 15:09

JohnAndrews


People also ask

How do I make my angular site SEO friendly?

A simple workaround to this problem is to use a dynamic rendering tool that helps create static HTML files for web crawlers to index easily. You can then set up your web server to send the search engine crawlers to the pre-rendered pages, and the visitors will be redirected to your regular Angular website.

Do URL slugs matter for SEO?

Are Web Slugs Important for Search Engine Ranking? The short answer is yes. Using SEO keywords in your URL can help you rank for your target keywords. Google uses the URL as a factor in ranking your page, so if the URL slug includes your keywords, then Google will be more likely to rank it.

What is slug in angular?

A custom AngularJS filter for converting a string into a url slug. A slug is a url & seo friendly string containing only lowercase alphanumeric characters and hyphens "-". Slugs are often used for including titles in URLs for SEO. (function () { 'use strict'; angular . module('app') .


1 Answers

You need to use the html5Mode for that

In web.config add

$locationProvider.html5Mode(true);

In index.html page

<base href="/">

It is enough but when you will refresh the page it will give the error so need to use url re-writing, so add in web.config

<system.webServer>
    <rewrite>
     <rules> 
      <rule name="Main Rule" stopProcessing="true">
        <match url=".*" />
        <conditions logicalGrouping="MatchAll">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />                                 
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
        <action type="Rewrite" url="/" />
      </rule>
    </rules>
    </rewrite>
</system.webServer>

And your state will need to change, like this

.state('app.browse-detail', {
      url: '/browse/:productId/:slug',
      templateUrl: 'templates/browse-detail.html',
      controller:'BrowseDetailCtrl as detail',

For more detail see AngularJS HTML5 mode reloading page not found solution

like image 152
Ali Adravi Avatar answered Sep 21 '22 19:09

Ali Adravi