another way to handle what to display after the hash (so if you don't use pushstate !) is to create your customlocation and load it at reactrouter creation.

for exemple, if you want to have hashbang url (so with #!) to comply with google specs for crawling, your can create a hashbanglocation.js file which mainly copy the original hashlocation such as :

'use strict';

var locationactions = require('../../node_modules/react-router/lib/actions/locationactions');
var history = require('../../node_modules/react-router/lib/history');

var _listeners = [];
var _islistening = false;
var _actiontype;

function notifychange(type) {
  if (type === locationactions.push) history.length += 1;

  var change = {
    path: hashbanglocation.getcurrentpath(),
    type: type

  _listeners.foreach(function (listener) {, change);

function slashtohashbang(path) {
  return "!" + path.replace(/^\//, '');

function ensureslash() {

  var path = hashbanglocation.getcurrentpath();
  if (path.charat(0) === '/') {
    return true;
  }hashbanglocation.replace('/' + path);

  return false;

function onhashchange() {
  if (ensureslash()) {
    // if we don't have an _actiontype then all we know is the hash
    // changed. it was probably caused by the user clicking the back
    // button, but may have also been the forward button or manual
    // manipulation. so just guess 'pop'.
    var curactiontype = _actiontype;
    _actiontype = null;
    notifychange(curactiontype || locationactions.pop);

 * a location that uses `window.location.hash`.
var hashbanglocation = {

  addchangelistener: function addchangelistener(listener) {

    // do this before listening for hashchange.

    if (!_islistening) {
      if (window.addeventlistener) {
        window.addeventlistener('hashchange', onhashchange, false);
      } else {
        window.attachevent('onhashchange', onhashchange);

      _islistening = true;

  removechangelistener: function removechangelistener(listener) {
    _listeners = _listeners.filter(function (l) {
      return l !== listener;

    if (_listeners.length === 0) {
      if (window.removeeventlistener) {
        window.removeeventlistener('hashchange', onhashchange, false);
      } else {
        window.removeevent('onhashchange', onhashchange);

      _islistening = false;

  push: function push(path) {
    _actiontype = locationactions.push;
    window.location.hash = slashtohashbang(path);

  replace: function replace(path) {
    _actiontype = locationactions.replace;
    window.location.replace(window.location.pathname + + '#' + slashtohashbang(path));

  pop: function pop() {
    _actiontype = locationactions.pop;

  getcurrentpath: function getcurrentpath() {
    return decodeuri(
    // we can't use window.location.hash here because it's not
    // consistent across browsers - firefox will pre-decode it!
    "/" + (window.location.href.split('#!')[1] || ''));

  tostring: function tostring() {
    return '<hashbanglocation>';


module.exports = hashbanglocation;

note the slashtohashbang function.

then you juste have to do

reactrouter.create({location: hashbanglocation})

and that's it :-)


install the history package

npm install history --save

next import createhistory and usebasename from the history

import { createhistory, usebasename } from 'history';
const history = usebasename(createhistory)({
  basename: '/root' 

if your app url is, then /root should be /myapp.

pass the history variable to the router

  <router history={history}>
), document.getelementbyid('example'));

now for all your link tags append a "/" in front of all the paths.

<link to="/somewhere">somewhere</link>

the inspiration of the solution came from react-router example which unfortunately, was not properly documented in their api.


you can actually use .htaccess to accomplish this. the browser normally needs the query string delimiter ? or # to determine where the query string begins and the directory paths end. the end result we want is so we need to catch the issue before the web server searches for the directory it thinks we asked for /dir. so we place a .htaccess file in the root of the project.

    # setting up apache options
    adddefaultcharset utf-8
    options +followsymlinks -multiviews -indexes
    rewriteengine on

    # setting up apache options (godaddy specific)
    #directoryindex index.php
    #rewritebase /

    # defining the rewrite rules
    rewritecond %{script_filename} !-d
    rewritecond %{script_filename} !-f

    rewriterule ^.*$ ./index.html

then you obtain the query parameters with window.location.pathname

you can then avoid using react routes if you want and just manipulate the url and browser history if you want as well. hope this helps someone...


if you don't need to support ie8, you can use browser history and react-router will use window.pushstate instead of setting the hash.

how exactly to do this depends on the version of react router that you are using:

score:40, router.historylocation, function (handler) {
  react.render(<handler/>, document.body);

for the current version 0.11 and forward, you need to add router.historylocation to <routes> are now deprecated. see the upgrade guide for the 0.12.x historylocation implementation.


for the versions 1, 2 and 3 of react-router, the correct way to set the route to url mapping scheme is by passing a history implementation into the history parameter of <router>. from the histories documentation:

in a nutshell, a history knows how to listen to the browser's address bar for changes and parses the url into a location object that the router can use to match routes and render the correct set of components.

versions 2 and 3

in react-router 2 and 3, your route configuration code will look something like this:

import { browserhistory } from 'react-router'
reactdom.render (( 
 <router history={browserhistory} >
), document.body);

version 1

in version 1.x, you will instead use the following:

import createbrowserhistory from 'history/lib/createbrowserhistory'
reactdom.render (( 
  <router history={createbrowserhistory()} >
), document.body);

source: version 2.0 upgrade guide

version 4

for the upcoming version 4 of react-router, the syntax has changed a great deal and it is required is to use browserrouter as the router root tag.

import browserrouter from 'react-router/browserrouter'
reactdom.render (( 
), document.body);

source react router version 4 docs

Related Query

More Query from same tag