Redirecting all subdirectories of a folder via htaccess [duplicate]

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?


Solution :

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.)



Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect