Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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)
share|improve this question

1 Answer 1

up vote 3 down vote accepted

You need to remove the carats from your RewriteCond lines:

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

The rewrite conditions will then take effect and you'll avoid the infinite redirect loop. This is required because your old rewrite condition will never take effect.

Your original RewriteCond %{THE_REQUEST} !^subdir/.*\-new\-name\.html rule says "If the request does not start with the string "subdir...". But %{THE_REQUEST} variable contains the full HTTP request, which looks like "GET /subdir/file.html HTTP/1.1". It will never begin with "subdir", because it always begins with "GET".

One alternative would be to use %{REQUEST_URI} with a leading slash for the subdirectory name:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/subdir/.*\-new\-name\.html
RewriteCond %{REQUEST_URI} !^/subdir/$
RewriteRule ^subdir/(.*)\.html$ http://todoodlist.com/subdir/$1\-new\-name\.html [NC,R=301]
share|improve this answer
    
works like a charm –  Howard Davis Oct 3 '12 at 22:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.