BarryWalsh
BarryWalsh

Reputation: 1340

Ajax crawable content .htaccess rewrite rule

I've been trying to figure this one out the past couple of hours but can't get it to work.

I'm tyring to send google to a server side generated version of each page of my AngularJS app.

My angular URLs look like the following:

http://localhost:8000/#!/product/123/product+name

The static versions generated have the following URL stucture:

http://localhost:8000/product/123/product+name

So they are both quite similar. I've tried a few different rewrite rule configurations but none have worked so far. For example:

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

Any help would be greatly appreciated. Thanks!

EDIT:

I forgot to say Google and other search engines convert the hash bang URLs into URLs like

http://localhost:8000/?_escaped_fragment_=/product/123/product+name

Current .htaccess file:

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

RewriteCond %{REQUEST_URI}  ^/$
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/$ [NC]
RewriteRule ^ /snapshots/index.html? [NC,L]

RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
RewriteRule ^(.*)$ /%1? [NC,PT]
</IfModule>

Upvotes: 1

Views: 536

Answers (1)

Justin Iurman
Justin Iurman

Reputation: 19016

I think i found the problem.

Try using PT flag instead of L flag because of your internal redirect on symlink.
This way, it is re-evaluated

RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
RewriteRule ^(.*)$ /%1? [NC,PT]

EDIT: You have to reorder your rules. Your htaccess should look like this

<IfModule mod_rewrite.c>
   <IfModule mod_negotiation.c>
      Options -MultiViews
   </IfModule>

   RewriteEngine On

   RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
   RewriteRule ^(.*)$ /%1? [NC,PT]

   # Redirect Trailing Slashes...
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)/$ /$1 [L,R=301]

   # Handle Front Controller...
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^ index.php [L]
</IfModule>

Upvotes: 1

Related Questions