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:


The new pages are named:


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.


We also need to be able to access the url:


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:


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!


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


1 Answer 1


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]
  • works like a charm Commented Oct 3, 2012 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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