Forum Moderators: phranque
I have around 80 folders
an example folder i'll call hedge-hog
hedge-hog contains the following files
hedge-hog-cookers.html
hedge-hog-grills.html
123.html
123-4567.html
123-324-25-2.html
123-22.html
993.html
993-4567.html
993-324-25-2.html
993-22.html
what I need to do is redirect all the files that don't start with hedge-hog back to the first number/word group that appears in them
eg these
123-4567.html
123-324-25-2.html
123-22.html
are redirected back to
123.html
however all files that start with hedge-hog should be left alone, I have the below rule that works fine
rewriteCond %{REQUEST_URI}!^/hedge-hog/hedge-hog-([^/\.]+)\.html$
RewriteRule ^hedge-hog/([^/\.]+)-([^/\.]+)\.html$ /hedge-hog/$1.html [R=301,L]
However i have to type this rule out 80 times, once each for each of the affected directories, anyone know of an easier way? as all I have come up with is the below but it seems a shade clunky (there would be 80 different rewrite conditions)
rewriteCond %{REQUEST_URI}!^/tuatara/tuatara-([^/\.]+)\.html$
rewriteCond %{REQUEST_URI}!^/zebu/zebu-([^/\.]+)\.html$
rewriteCond %{REQUEST_URI}!^/hedge-hog/hedge-hog-([^/\.]+)\.html$
rewriteCond %{REQUEST_URI}!^/...etc...
RewriteRule ^([^/]+)/([^/\.]+)-([^/\.]+)\.html$ /$1/$2.html [R=301,L] I should add that the files to be left alone have the exact name of the directory they are in as their prefix
as in all files in the hedge-hog directory that start with hedge-hog are to be left alone all files in the zebu directory that start with zebu should be left alone etc
Any help would be gratefully received
The basic problem is that there is no "compare" function available in mod_rewrite that is portable across all operating systems, so the fact that the filename and the directory name match isn't directly-testable. (There is a way to do it, but it's not supported by all OSes, and is rather hard to construct (and ugly).
You've got some unnecessary parentheses and character-escaping, and a RewriteRule subpattern that is non-optimal though. So before you start the cut-n-paste task, try this cleaned-up version, and if it meets your requirements, you can then replicate it:
RewriteCond %{REQUEST_URI} !^/tuatara/tuatara-[^/.]+\.html$
RewriteCond %{REQUEST_URI} !^/zebu/zebu-[^/.]+\.html$
RewriteCond %{REQUEST_URI} !^/hedge-hog/hedge-hog-[^/.]+\.html$
RewriteCond %{REQUEST_URI} !^/...etc...
RewriteRule ^([^/]+)/([^\-]+)-[^/.]+\.html$ /$1/$2.html [R=301,L]
Jim