1

I want to match all subpages to parent pages, am using wordpress, will that mess up the images and wordpress files?, And I have tags and categories, and I want to exclude them from this rule.

RewriteRule ^(.*/). https://www.example.com/$1 [R=301,L]

This is the htaccess rule. Let's say I want to exclude https://example.com/year/1999,https://example.com/year/1979, etc from this rule. How do I do that?

1 Answer 1

0
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?)

3
  • Yes, This is a permanent move, But forgot to mention one thing, Am movie Site A from Site B, and Site B won't have all the subpages that were from Site A. But will have parent pages from the Site A. Will this work still?. And also exclude anything after /year/, /year/(.+/) will do yes? Jan 28, 2023 at 2:48
  • @mutamilvativu Providing Site B and Site A point to different hosts then that is OK. In fact, this rule is dependent on redirecting to a different host (otherwise there will be successive redirects to the "parent" folder, until you're at the top). Currently this rule excludes any URL that starts /year/1999 or /year/1979 (as stated in your question).
    – MrWhite
    Jan 28, 2023 at 11:16
  • @mutamilvativu To exclude any URL that starts /year/ then just use !^/year/. /year/(.+/) only matches /year/<something>/<anything> and not /year/<anything>.
    – MrWhite
    Jan 28, 2023 at 11:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .