2

I've searched around and haven't found this specific question asked.

I need to redirect all the 'subfolders' of a directory to the parent directory.

For example, the parent directory is example.tld/directory and all subdirectories such as example.tld/directory/subdirectory-1 and example.tld/directory/subdirectory-2 etc all need to redirect to example.tld/directory

For clarification, these aren't actual directories but Drupal sub-paths, but I believe it should work to do this via htaccess.

Just to make it crystal clear, the URLs

example.tld/directory/subdirectory-1

example.tld/directory/subdirectory-2

etc...

should all redirect to

example.tld/directory

The idea is to make these subdirectories inaccessible to anyone and be sure that the search engine crawlers do not index these subdirectories.

What would be the proper htaccess code to achieve this?

2

After more searching and digging, I did find that this question had been asked, I just didn't know how to word it!

Here's where I found the answer:

301 redirect all child pages to parent directory using .htaccess

Here is what the answer is - I tested it and it works perfectly:

Near the top of the .htaccess file in the root of your site, you can add the following:

RewriteEngine On
RewriteRule ^(parent/). /$1 [R,L]

The $1 is just a back-reference to parent/ (saves repetition).

Change R to R=301 if this is intended to be permanent. (But only after you have confirmed that it works.)

| improve this answer | |
  • Ha, thought I'd answered something similar before - glad you found it! (Once you have 15+ rep you can also upvote other answers you find useful. ;) – MrWhite Feb 26 '19 at 16:45
  • Cool, you've got a great resource here! – Chapman Woodriff Feb 27 '19 at 11:41

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