0

I use angular with html5 mode

 $routeProvider.otherwise({redirectTo : '/index' });
 $locationProvider.html5Mode(true).hashPrefix('!');

and for that I have htaccess to handle google indexing

 # Rewrite anything with google appended string to english version of the snapshot
 RewriteCond %{QUERY_STRING} ^_escaped_fragment_=
 RewriteCond %{HTTP_HOST}  ^touchtyping.guru [NC]
 RewriteRule ^ snapshots%{REQUEST_URI}-en.html [L]

 # Rewrite everything else to index.html to allow html5 state links
 RewriteRule ^ index.html [QSA,L]

This redirects to appropriate snapshot for all the subpages, like eg:

 http://touchtyping.guru/learn-touch-typing?_escaped_fragment_=
 http://touchtyping.guru/index?_escaped_fragment_=

but it doesn't work for the root domain like below, rendering the /index page, instead of redirecting to it's snapshot:

 http://touchtyping.guru/?_escaped_fragment_=

The snapshot exists, I tried hardcoded index-en.html there, but no success. Seems like htaccess doesn't catch the query string in this case, loading angular routing. How can I fix that?

UPDATE--------------------------------------

The whole htaccess file:

 RewriteEngine on

 # Don't rewrite files or directories
 RewriteCond %{REQUEST_FILENAME} -f [OR]
 RewriteCond %{REQUEST_FILENAME} -d
 RewriteRule ^ - [L]

 # Redirect http://www. to just http://
 RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
 RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

 RewriteCond %{QUERY_STRING} ^_escaped_fragment_=
 RewriteCond %{HTTP_HOST}  ^touchtyping.guru [NC]
 RewriteRule ^ snapshots%{REQUEST_URI}-en.html [L]

 RewriteCond %{QUERY_STRING} ^_escaped_fragment_=
 RewriteCond %{HTTP_HOST}  ^(es|pl|en).touchtyping.guru [NC]
 RewriteRule ^ snapshots%{REQUEST_URI}-%1.html [L]

 # Rewrite everything else to index.html to allow html5 state links
 RewriteRule ^ index.html [QSA,L]
dzezzz
  • 985
  • 7
  • 17
  • Just wanted to point out that google will index your application in html5 mode without the escaped fragments and snapshots (bing supposedly does too, though i haven't taken the time to prove that yet.) The downside is that google will not follow the sref links, so you'd have to submit a sitemap. – Kevin B Jan 23 '15 at 15:30
  • Unfortunately, it doesn't, you can check yourself typing in google 'site:touchtyping.guru' - you'll see that the main page that has no snapshots has not rendered chars in description... – dzezzz Jan 23 '15 at 15:56
  • My angularjs site using html5mode is being indexed properly, i can't comment on anyone elses. – Kevin B Jan 23 '15 at 15:58

1 Answers1

1

Have you tried adding a specific rule for that?

 RewriteCond %{QUERY_STRING} ^_escaped_fragment_=
 RewriteCond %{HTTP_HOST}  ^touchtyping.guru [NC]
 RewriteRule ^$ snapshots/index-en.html [L]

EDIT:

Try re-arranging your rules a bit:

 RewriteEngine on

 # Redirect http://www. to just http://
 RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
 RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

 RewriteCond %{QUERY_STRING} ^_escaped_fragment_=
 RewriteCond %{HTTP_HOST}  ^touchtyping.guru [NC]
 RewriteRule ^(index\.html)?$ snapshots/index-en.html [L]

 RewriteCond %{QUERY_STRING} ^_escaped_fragment_=
 RewriteCond %{HTTP_HOST}  ^(es|pl|en).touchtyping.guru [NC]
 RewriteRule ^(index\.html)?$ snapshots/index-%1.html [L]

 # Don't rewrite files or directories
 RewriteCond %{REQUEST_FILENAME} -f [OR]
 RewriteCond %{REQUEST_FILENAME} -d
 RewriteRule ^ - [L]

 RewriteCond %{QUERY_STRING} ^_escaped_fragment_=
 RewriteCond %{HTTP_HOST}  ^touchtyping.guru [NC]
 RewriteRule ^ snapshots%{REQUEST_URI}-en.html [L]

 RewriteCond %{QUERY_STRING} ^_escaped_fragment_=
 RewriteCond %{HTTP_HOST}  ^(es|pl|en).touchtyping.guru [NC]
 RewriteRule ^ snapshots%{REQUEST_URI}-%1.html [L]

 # Rewrite everything else to index.html to allow html5 state links
 RewriteRule ^ index.html [QSA,L]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • yes, "I tried hardcoded index-en.html", it exists under http://touchtyping.guru/snapshots/index-en.html but no success – dzezzz Jan 23 '15 at 15:58
  • @dzezzz well, can't really help if we don't know what your htaccess file looks like, the order of the rules matter so if they aren't in the right order, it's not going to work – Jon Lin Jan 23 '15 at 16:36
  • I've just added full htaccess – dzezzz Jan 23 '15 at 23:09
  • @dzezzz do you have an index.html file in your document root? If one is there, then mod_rewrite STOPS at your very first rule, and nothing after it gets evaluated at all – Jon Lin Jan 23 '15 at 23:13
  • yes, index.html is in document root, snapshots folder next to it, seems logical what you say, though still the same problem - it doesn't redirect the root domain... – dzezzz Jan 23 '15 at 23:28
  • @dzezzz another edit, only other thing I can think of, the rules work fine for me in a vanilla apache 2.2 install, everything including the root routes to snapshots (which is 404 for me) when I have the fragment, have you cleared your cache? – Jon Lin Jan 23 '15 at 23:32
  • Cache was cleared, though after this update it works, thank you very much ;-) – dzezzz Jan 24 '15 at 10:28