Score:1

htaccess redirect all subpages to parent page except for some parent pages

us flag

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?

Score:0
kz flag
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?)

mutamilvativu avatar
us flag
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?
kz flag
@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).
kz flag
@mutamilvativu To exclude any URL that starts `/year/` then just use `!^/year/`. `/year/(.+/)` only matches `/year/<something>/<anything>` and not `/year/<anything>`.
mutamilvativu avatar
us flag
Thanks, that worked.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.