Search code examples
.htaccessmod-rewritehttp-redirect

301 redirect - Redirect parent but not it's children


How would I go about re-directing a parent directory but not children? See my specific issue below. All and any help is appreciated!

Parent -

redirect 301 /community/whats-happening http://www.stapletondenver.com/whats-happening/  
  • redirects parent directory

Child -

redirect 301 /community/whats-happening/celebrating-halloween-stapleton http://www.stapletondenver.com/whats-happening/celebrating-halloween-in-stapleton/  

I have tried a handful of other options to no success. See below:

RewriteRule  ^community/whats-happening/. /whats-happening/ [R=301,L]

RewriteRule ^community/whats-happening/(.*)$ /whats-happening/$1 [R=301,NC,L]

RewriteRule ^/?community/whats-happening/?$ http://www.stapletondenver.com/whats-happening/ [R=301]

RewriteRule ^community/whats-happening/(.*) http://stapletondenver.com/whats-happening/$1 [R=301,L]
RewriteRule ^community/whats-happening/(.*)/(.*)/? http://stapletondenver.com/whats-happening/$1/ [R=301,L]

Solution

  • This is happening because of your first redirect. The Redirect directive redirects all subdirectories and files to, so:

    Redirect 301 /abcd http://domain.com/xyz
    

    means

    • /abcd/ -> http://domain.com/xyz/
    • and /abcd/1234/ -> http://domain.com/xyz/1234/
    • and /abcd/1234/z.html -> http://domain.com/xyz/1234/z.html

    etc.

    If you only want the parent directory to redirect, use a regex and RedirectMatch with a $ end character:

    RedirectMatch 301 ^/community/whats-happening/?$ http://www.stapletondenver.com/whats-happening/  
    RedirectMatch 301 ^/community/whats-happening/celebrating-halloween-stapleton/?$ http://www.stapletondenver.com/whats-happening/celebrating-halloween-in-stapleton/