RewriteRule ^\d{4}/\d{2}/(.*).html+.* http://www.example.com/$1/ [R=301,L]
This does look as if it should “work”, although there are a few things that can be tidied, and the query string (ie. m=1
or m=0
) will remain on the URL.
Also, make sure you have cleared your browser cache before testing as 301s are cached hard by the browser. It can be easier to test with 302 (temporary) redirects for this reason and change to a 301 only when you are sure it’s working OK. (Tip: Test with the browser’s Object Inspector open and the “Disable Cache” option checked.)
This redirect also needs to go at the top of your .htaccess
file, before the WordPress front-controller (ie. before the # BEGIN WordPress
section).
Try the following instead:
RewriteRule ^\d{4}/\d{2}/(.+)\.html$ http://www.example.com/$1/? [R=301,L]
The trailing +.*
on your pattern doesn’t make a lot of sense (although it shouldn’t cause a problem), as this simply repeats the l
(of html
) 1 or more times, followed by anything. The .html
is always the end of the URL-path (the query string is not part of the URL-path), so this should really be followed by $
(end-of-string anchor).
The ?
on the end of the RewriteRule
substitution effectively strips the query string from the target URL by creating an “empty” query string. Alternatively, on Apache 2.4+, you can use the QSD
flag instead (Query String Discard). ie. ... http://www.example.com/$1/ [QSD,R=301,L]
UPDATE: Is it safe to use …
Well, you could be more specific with the regex, so to make sure that it doesn’t match “too much” and only matches the URLs you are interested in. For example, the above directive will also match URLs of the form: example.com/2017/02/foo/bar/post-name.html
, which would result in a (possibly invalid) redirect to /foo/bar/post-name
. Or even example.com/2017/02/1111/22/post-name.html
, which would result in a double redirect. This may not matter at all, in fact, this might even be desirable depending on your circumstances. However, you could restrict the post-name
to just word characters (a-z
, A-Z
, 0-9
and _
) and hyphens by changing the RewriteRule
pattern. For example:
RewriteRule ^\d{4}/\d{2}/([\w-]+)\.html$ http://www.example.com/$1/? [R=301,L]
But otherwise, I can’t see that it would be “unsafe”.