RewriteRule ^(.*/). https://www.example.com/$1 [R=301,L]
will that mess up the images and wordpress files?
Yes, as written that rule would strip the filename for all requests to your static assets (images, CSS, JS, etc.). You need to exclude such requests (perhaps easiest to exclude any requests that map to a physical file).
Also note that this is currently dependent on you redirecting to a different host (eg. from Site-A to Site-B as stated in comments). Otherwise you will experience a redirect-loop that eventually takes you to the top "folder". eg. /foo/bar/baz/qux
to /foo/bar/baz/
For example, the following should go near the top of the root .htaccess
file, before the WordPress code block.
RewriteCond %{REQUEST_URI} !^/year/19(79|99)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+/). https://www.example.com/$1 [R=302,L]
The first condition excludes any URL that starts /year/1999
or /year/1979
. The second condition excludes any requests that map to a physical file (eg. your static assets).
The !
prefix on the CondPattern negates the expression, so it is successful when the expression does not match.
I did just change the RewriteRule
pattern from (.*/)
to (.+/)
since there is always going to be at least 1 character, never 0, before the slash. Not that this actually makes any real difference, but it is clearer to read IMO.
Note that after WordPress rewrites the request to the front-controller (ie. /index.php
), the rewrite engine effectively starts over and the above rule is processed a second time. However, on this second pass the RewriteRule
pattern ^(.+/).
will not match so nothing extra needs to be done in this regard.
You need to first test this with a 302 (temporary) redirect to avoid potential caching issues. (Is this really intended to be permanent?)