RewriteEngine on RewriteCond %{REQUEST_URI} !^.*/mobile/.*$ [PT] RewriteCond %{REQUEST_URI} !^.*/index.html$ [PT] RewriteCond %{REQUEST_URI} ^.*\.html$ [PT] RewriteRule /([a-z]+)-*(.*)\.html$ /#$1/$2 [R=301,L,NE,QSA]Works perfectly* except in case of filenames without a hyphen, it adds a trailing slash. Which I don't want & can't figure out why it's doing that or how to stop it.
RewriteEngine on #hyphenated RewriteCond %{REQUEST_URI} !^.*/mobile/.*$ [PT] RewriteCond %{REQUEST_URI} !^.*/index.html$ [PT] RewriteCond %{REQUEST_URI} ^.*\.html$ [PT] RewriteCond %{REQUEST_URI} ^.*/.*-.*\.html$ [PT] RewriteRule /([a-z]+)-(.*)\.html$ /#$1/$2 [R=301,L,NE,QSA] #not RewriteCond %{REQUEST_URI} !^.*/mobile/.*$ [PT] RewriteCond %{REQUEST_URI} !^.*/index.html$ [PT] RewriteCond %{REQUEST_URI} ^.*\.html$ [PT] RewriteRule /([a-z]+)\.html$ /#$1 [R=301,L,NE,QSA]
RewriteRule ^.*/mobile/ - [L,PT] RewriteRule ^.*/index.html$ - [L,PT] RewriteRule /([a-z]+)-([^-]+)\.html$ /#$1/$2 [L,R=301,NE,QSA] RewriteRule /([a-z]+)\.html$ /#$1 [L,R=301,NE,QSA]
Possibly a clearer way of explaining is that RewriteCond is not like an IF statement, but rather it's additional filtering checked only if the RewriteRule pattern is a match (but before the replacement/rewriting occurs).
...RewriteCond directives can be used to restrict the types of requests that will be subject to the following RewriteRule.
It is disappointing the docs don't mention it at all - since it's not obvious, but anyhow the easiest way to prove it is the source: apply_rewrite_rule in mod_rewrite.c
RewriteRule matching is preceded by this comment...
/* Try to match the URI against the RewriteRule pattern * and exit immediately if it didn't apply. */
And after that we have this...
/* Ok, we already know the pattern has matched, but we now * additionally have to check for all existing preconditions * (RewriteCond) which have to be also true. We do this at * this very late stage to avoid unnecessary checks which * would slow down the rewriting engine. */
Curious to see performance given as the reason, since arguably simpler string header checks could be cheaper than the convoluted regexes that can occur - having the option to choose when the condition applied would allow the best performance.
Is it not simpler to use robots.txt to block them?
RewriteEngine On RewriteBase / RewriteCond %{HTTP_USER_AGENT} !google|yahoo|bing [NC] RewriteCond %{HTTP_REFERER} !google|yahoo|bing [NC] RewriteCond %{REQUEST_URI} !^.*/mobile/ RewriteCond %{REQUEST_FILENAME} !^/index.html$ RewriteRule ^([a-z]+)-(.+)\.html$ /#$1/$2 [NE,R=301,L] RewriteCond %{REQUEST_URI} !^.*/#[a-z]+/[.*]$ RewriteRule ^([a-z]+)\.html$ /#$1 [NE,R=301,L]Staging server on Ubuntu 14.04 ppc (powermac G4), hosted in an "seo2" subdirectory:
RewriteEngine On RewriteBase / RewriteCond %{HTTP_USER_AGENT} !google|yahoo|bing [NC] RewriteCond %{HTTP_REFERER} !google|yahoo|bing [NC] RewriteCond %{REQUEST_URI} !^.*/mobile/.*$ RewriteCond %{REQUEST_URI} !^.*/index.html$ RewriteRule ([a-z]+)-(.+)\.html$ /seo2/#$1/$2 [NE,R,L] RewriteCond %{HTTP_USER_AGENT} !google|yahoo|bing [NC] RewriteCond %{HTTP_REFERER} !google|yahoo|bing [NC] RewriteCond %{REQUEST_URI} !^.*/mobile/.*$ RewriteCond %{REQUEST_URI} !^.*/#[a-z]+/[.*]$ RewriteRule ([a-z]+)\.html$ /seo2/#$1 [NE,R,L]Not found any good online htaccess documentation or tutorials (relied a lot on stackoverflow), so these evolved through a lot of trial and (mostly) error.