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.

  • If you can, do your rewriting inside Apache main configuration instead of `.htaccess` files as this will prove simpler and more secure. Also `[ˆ.html]` means any character not being . h t m or l. I suppose you were instead aiming for `(\.html)?` which means the whole extension is optional. In your first `RewriteRule` you do not capture anything in parentheses, so `$1` will never be set. – Patrick Mevzek Dec 20 '17 at 18:45
  • I need to remove the .html, as on the new server the URI is not a .html document. Thanks for the parentheses tip. – Pete Mallam Dec 20 '17 at 19:19
  • According to http://htaccess.madewithlove.be/ This *should* work: `RewriteCond %{REQUEST_URI} ^/oldfolder/` `RewriteRule ^/oldfolder/(.*).html$ newfolder1/$1 [L]` – Pete Mallam Dec 20 '17 at 20:32
  • "This works fine" - Unless something is missing in your exemplified code then that would never have worked as intended? All the conditions (`RewriteCond` directives) are the same, so only the first rule would ever get processed. (?) – MrWhite Dec 20 '17 at 23:00

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?

  • "Wordpress will replace the 'folder name' for me" - But presumably that is a _second_ redirect? "will perform a 301 redirect" - The directive you posted is a 302 (temporary) redirect. You need to explicitly include the status code if you want a 301. eg. `RedirectMatch 301 ...`. However, if this is WordPress, then you should probably be using mod_rewrite instead (`RedirectMatch` is a mod_alias directive). Different modules execute at different times (mod_rewrite first), despite the aparent order of the directives in `.htaccess`, so you can end up with unexpected conflicts. – MrWhite Dec 20 '17 at 23:12