AngularJS SEO htaccess: redirect root domain to snapshot

此生再无相见时 提交于 2019-12-12 06:12:26

问题


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]

回答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]


来源:https://stackoverflow.com/questions/28113109/angularjs-seo-htaccess-redirect-root-domain-to-snapshot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!