3

I have a requirement from a customer to perform some specific redirects (part of decomissioning an old server to a new one). I've been banging my head against this and it's driving me nuts!

In effect, I need to do two .htaccess mod-rewrites at the same time. I'm expecting this will be something really simple, but I've tried everything I can think of so it's time to ask for advice.

I need to do the following:

To further complicate, this is repeated due to the webresources may be in different newfolder locations (newfolder1, newfolder2, newfolder3).

At the moment, my .htaccess segment looks like this:

    <IfModule mod_rewrite.c>
       RewriteEngine on

       RewriteCond         "%{DOCUMENT_ROOT}/oldfolder/%{REQUEST_URI}"  -f
       RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/newfolder1/$1"            [L]
       RewriteCond         "%{DOCUMENT_ROOT}/oldfolder/%{REQUEST_URI}"  -f
       RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/newfolder2/$1"            [L]
       RewriteCond         "%{DOCUMENT_ROOT}/oldfolder/%{REQUEST_URI}"  -f
       RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/newfolder3/$1"            [L]
       RewriteRule   "^"  "-"  [PT]

This works fine to redirect the folder but it only works if some-web-resource does not already contain the .html.

I have tried:

RewriteRule "%{REQUEST_URI}[^.html]" "%{DOCUMENT_ROOT}/newfolder1/$1"  [L]
RewriteRule "^(.*).html$" "%{DOCUMENT_ROOT}/newfolder1/$1"  [L]

and many variations on this, but I'm not getting anywhere.

1 Answers1

3

Okay, so as it turns out Wordpress (the new system) will do a lot of the heavy lifting form me and I was barking up the wrong tree.

The OLD URL (http://example.com/oldfolder/some-web-resource.html) has the html extension and by way of a mistake I realised that this is the only thing I need to change - Wordpress will replace the 'folder name' for me as part of the "yeah, you got a bad URL there matey" check.

So my fix was as simple as throwing the following line in at the top of the .htaccess file: RedirectMatch "^(/oldfolder/.*).html" "$1"

This now, if the URL requested is http://example.com/oldfolder/blah.html, will perform a 301 redirect using http://example.com/oldfolder/blah and then Wordpress will do the rest, changing to newfolder1, newfolder2, newfolder3 etc as needed based on the remainder of the URI being requested.

Would you believe that took me 4 hours to figure out?