How can I redirect all files in a directory that doesn't conform to a certain filename structure?

Posted by user18842 on Pro Webmasters See other posts from Pro Webmasters or by user18842
Published on 2012-10-03T04:29:18Z Indexed on 2012/10/03 9:50 UTC
Read the original article Hit count: 221

I have a website where a previous developer had updated several webpages. The issue is that the developer had made each new webpage with new filenames, and deleted the old filenames. I've worked with .htaccess redirects for a few months now, and have some understanding of the usage, however, I am stumped with this task.

The old pages were named like so:

www.domain.tld/subdir/file.html

The new pages are named:

www.domain.tld/subdir/file-new-name.html

The first word of all new files is the exact name of the old file, and all new files have the same last 2 words.

www.domain.tld/subdir/file1-new-name.html
www.domain.tld/subdir/file2-new-name.html
www.domain.tld/subdir/file3-new-name.html
ect.

We also need to be able to access the url:

www.domain.tld/subdir/

The new files have been indexed by google (the old urls cause 404s, and need redirected to the new so that google will be friendly), and the client wants to keep the new filenames as they are more descriptive. I've attempted to redirect it in many different ways without success, but I'll show the one that stumps me the most

RewriteBase /
RewriteCond %{THE_REQUEST} !^subdir/.*\-new\-name\.html
RewriteCond %{THE_REQUEST} !^subdir/$
RewriteRule ^subdir/(.*)\.html$ http://www.domain.tld/subdir/$1\-new\-name\.html [R=301,NC]

When visiting www.domain.tld/subdir/file1.html in the browser, this causes a 403 Forbidden error with a url like so:

www.domain.tld/subdir/file1-new-name-new-name-new-name-new-name-new-name-new-name-new-name-new-name-new-name-new-name-new-name-new-name-new-name.html

I'm certain it's probably something simple that I'm overlooking, can someone please help me get a proper redirect? Thanks so much in advance!

EDIT

I've also got all the old filenames saved on a separate document in case I need them set up like the following example:

(file(1|2|3|4|5)|page(1|2|3|4|5)|a(l(l|lowed|ter)|ccept)

© Pro Webmasters or respective owner

Related posts about htaccess

Related posts about mod-rewrite