Redirecting all subdirectories of a folder via htaccess [duplicate]
.htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, , , , .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :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?
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
.htaccessfile in the root of your site, you can add the following:RewriteEngine On
RewriteRule ^(parent/). /$1 [R,L]The
$1is just a back-reference toparent/(saves repetition).Change
RtoR=301if this is intended to be permanent. (But only after you have confirmed that it works.)
Comments
Post a Comment