I hope someone will be able to help me with this, here goes.
I have a couple of CNAMES pointed back to the primary domain for mod_pagespeed sharding, and I'm already adding www to the domain if there is no subdomain like this:
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
I have mod_pagespeed setup to automatically shard the file assets like this:
ModPagespeedShardDomain example.com static1.example.com,static2.example.com
ModPagespeedMapRewriteDomain example.com www.example.com
ModPagespeedMapOriginDomain localhost example.com
To eliminate the duplicate content penalty, I want to 301 redirect non-file, path only requests back to the primary domain:
static[1 or 2].example.com/varying/dir/structure -(301)->
www.example.com/varying/dir/structure
However I need to exclude any cases where there is a file.ext in the URL, so the requests would not be rewritten.
static[1 or 2].example.com/varying/dir/structure/file.of.any.ext <--ignored
Hope this is descriptive enough, any help is greatly appreciated, thanks.
You can use a rule like this as your first rule:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+\.[^.]+$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]*)/?$ http://www.example.com/$1 [L,R=301]