1

How would I go about 301 Redirecting all child pages to the parent page with the exception of a few child pages 301 Redirected to other specific pages throughout my site.

Below works for redirecting all the child pages to the parent page:

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

How would I then go about 301 Redirecting a specific child page of the above parent page to a different page on the site?

Example of the 301 Redirect I am trying to accomplish after redirecting all child pages to the parent page:

301 Redirect /parent-page/one https://www.example.com/parent-page-two/one
  • What is the problem you are having? You seem to have already done the hard bit. – MrWhite Sep 17 '18 at 20:44
  • The main RewriteRule seems to be overriding my individual 301 redirects for single pages. So instead of redirecting to the page I'm specifying, it goes to the parent page. Just a little loss if I'm missing some aspect in order to escape the main redirect – Dzen23 Sep 17 '18 at 21:09
  • Ah, sorry, is your last line your "individual 301 redirect"? Possibly a typo in your question, but you have the arguments round the wrong way - which would make it wholly invalid. (At a glance, I assumed that was just pseudo-code.) I've added an answer below. – MrWhite Sep 17 '18 at 23:05
0
301 Redirect /parent-page/one https://www.example.com/parent-page-two/one

(Incidentally, that should be Redirect 301 ....)

However, you shouldn't be using a mod_alias Redirect directive when you are already using mod_rewrite (ie. RewriteRule) for your general redirect. The mod_rewrite RewriteRule will always run first, regardless of the apparent order of your directives. If you are already using mod_rewrite then use mod_rewrite throughout for your redirects.

You should use a more specific RewriteRule before your existing "generic" RewriteRule. More specific directives should be first since the directives are processed in order top to bottom (within the same module).

For example:

# Enable the rewrite engine (mod_rewrite)
RewriteEngine On

# More specific redirects...
RewriteRule ^parent-page/one$ https://www.example.com/parent-page-two/one [R=301,L]

# Generic redirect last
RewriteRule ^(parent-page/). https://www.example.com/$1 [R=301,L]

You will need to clear your browser cache before testing, as any erroneous 301 (permanent) redirects are likely to have been cached by your browser. For this reason, it is often easier to first test with 302 (temporary) redirects to avoid caching issues and only switch to 301s (if that is the intention) once you have confirmed everything works.


However, if you aren't already using mod_rewrite for anything else then you can instead do this with the "simpler" mod_alias. For example:

# More specific redirects...
RedirectMatch 301 ^/parent-page/one$ https://www.example.com/parent-page-two/one

# Generic redirect last
RedirectMatch 301 ^/(parent-page/). https://www.example.com/$1

But don't mix redirects from both mod_alias and mod_rewrite as you can get unexpected conflicts.


Note that the mod_alias Redirect directive is prefix-matching, so this may or may not be suitable in your scenario. It depends if you have other URLs that start /parent-page/one that should not be redirected.


UPDATE: If you have just one domain, to which you are redirecting, and you have already canonicalised the scheme (HTTP or HTTPS) and hostname (example.com or www.example.com) then you don't necessarily need to specify an absolute URL in the substitution. For example, you could use a root-relative path and simplify the directive(s) to read:

# Generic redirect last
RewriteRule ^(parent-page/). /$1 [R=301,L]

etc.

  • 1
    This is perfect! Using the mod_rewrite RewriteRule route worked like a charm and I appreciate the insights on why my previous solution was not working. I have to redirect thousands of pages to clean up a site and only a handful are going to be specific page to page with child pages attached to them so this solution solves everything. Thank you @MrWhite – Dzen23 Sep 18 '18 at 11:16
  • You're welcome. You could perhaps remove the scheme + hostname in the substitution to simplify these redirects if you wish. I've updated my answer. – MrWhite Sep 18 '18 at 11:37

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.