0

I searched, read and experimented for the whole day but could not make it work.

In my angularjs site I have

$locationProvider.html5Mode(false);
$locationProvider.hashPrefix('!');

So, my links are like,

http://example.com/#!/home
http://example.com/#!/folder1/folder2

According to AngularJS SEO docs, crawlers will see above links as,

http://example.com/?_escaped_fragment_=/home
http://example.com/?_escaped_fragment_=/folder1/folder2

Now, my goal is to write some rules in .htaccess to redirect crawlers to some snapshot folder and static html files. So, when the crawlers try to go to the above links, they should be redirected to,

http://example.com/snapshot/home
http://example.com/snapshot/folder1/folder2

I have almost no experience with .htaccess files and very little knowledge of Perl Regex.

Please do not give only links as answers since I have googled and tried lots of combinations.

I also referred to this stackoverflow link, and experimented with my .htaccess file, but could not make it redirect to the snapshot folder.

Can anyone please help me make it work?

*********************** UPDATE 1 ********************************

As per @Olaf Dietsche's answer, I wrote these 2 lines (and these 2 only) in my .htaccess file

RewriteCond %{QUERY_STRING} _escaped_fragment_=([^&]*)
RewriteRule ^$ /snapshot/%1 [R,L]

The result is, when I am hitting

http://example.com/?_escaped_fragment_=/home

in my browser, the url is becoming

https://example.com:80/snapshot//home?_escaped_fragment_=/home

expected is http://example.com/snapshot/home

So, current issues are,

  1. the 'http' becomes 'https', hence getting SSL connection error
  2. a port number :80 comes suddenly, which was not there
  3. 2 forward slashes '//' after snapshot
  4. after 'home' the previously '?_escaped_fragment_=/home' also stays, which should not be there

Help please...

Community
  • 1
  • 1
Suman Barick
  • 3,311
  • 2
  • 19
  • 31
  • I have no idea, where the `https` or port `:80` comes from. There must be some other directives in your configuration. – Olaf Dietsche Oct 06 '15 at 18:16
  • I am on my own then, I guess :( ... Any pointer where I can do some research/study and be enlightened about the directives/configuration? – Suman Barick Oct 06 '15 at 20:36
  • 1
    I would first look into `access.log` and `error.log` to see if there's some hint. If there are additional redirects going on, you should see it in access.log. If there's a bug in the rules or something else wrong, you might see it in error.log – Olaf Dietsche Oct 06 '15 at 20:51
  • 1
    Maybe adding rewrite log output helps too. See [Logging](http://httpd.apache.org/docs/current/mod/mod_rewrite.html#logging) for how to do this. – Olaf Dietsche Oct 06 '15 at 20:55

1 Answers1

2

I am not familiar with Angular, but the reason why it is not redirecting, is because the rules in the quoted question only rewrite instead of redirect. See also Understanding difference between redirect and rewrite .htacess.

To redirect, you must either give an absolute URL to another domain as target or add the R|redirect flag, e.g.

RewriteCond %{QUERY_STRING} _escaped_fragment_=/([^&]*)
RewriteRule ^$ /snapshot/%1? [R,L]

The query string is appended to the target unaltered, unless you add your own query string. So, to remove the previous query string, you can just add a single question mark.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • Hi @Olaf, Thank you for your reply. Sorry about that redirect thing, what I need is to serve crawlers the static html as my site is entirely ajax based. I incorporated your changes into .htaccess file. But the problem is not fixed. I have updated my question with [UPDATE 1]. Guess I need a little more of your guidance and help. Thanks again for your patience. – Suman Barick Oct 06 '15 at 16:54
  • 1
    To avoid the double slash `//`, I adjusted the `RewriteCond`. The query string can be removed by adding a single question mark. See updated answer. – Olaf Dietsche Oct 06 '15 at 18:16
  • Thanks again. These 2 solution worked. Now I only have to figure out that SSL Error and port number thing. – Suman Barick Oct 06 '15 at 20:32