Forum Moderators: phranque

Message Too Old, No Replies

Making a more efficient generic redirect with an exclusion condition

I have it working and I think it could be shorter, but i don't know how

         

nigelt74

2:38 am on Oct 19, 2007 (gmt 0)

10+ Year Member



Ok this is the rule i am currently using

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

jdMorgan

3:08 am on Oct 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's not much help for a situation like this except to remember this trouble as you 'design' new directory names, filenames, and directory layouts in the future.

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]

When building the full list, I'd suggest you put the RewriteConds in order from most-requested to least-requested. Or at least put the top significant traffic-getters at the top of the list.

Jim

nigelt74

11:40 pm on Oct 19, 2007 (gmt 0)

10+ Year Member



Cheers for that, nice to know I was vaguely close with my redirect, just have to learn to make things more efficient

Thanks
Nigel