Redirecting subdirectory into root, and all pages therein

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
jnicola's picture

Hey there everyone,

So for a while there I was placing all of my drupal content in a subdirectory, and redirecting all my visitors to the subdirectory. I've come to discover through working with some people who do a lot of SEO work that it's not a good idea to go doing that. So, I moved everything to the root directory from the subdirectory, did abunch of other stuff for SEO, and now I want to redirect everything from the old sub directory back to the root directory.

The question is.... how? I looked a bunch of stuff online up, and can't quite seem to find a way to redirect. I can redirect from the /drupal to the root, but I can't seem to get it to work in a fashion that takes the previous /drupal/user and go to /user. The closest I've come yet in .htacces goes from /drupal/user to /?q=user, which with clean url isn't an option, and I really don't want search engines indexing that over the clean url.

So, anyone have any experience or advice on how to make this work with drupal? I've researched for a good hour and a half now, and haven't had any luck.

Thanks for your time,
Jesse

Comments

Jesse Nicola -- My Portfolio

jnicola's picture

Jesse Nicola -- My Portfolio -- MHSS, my main project!

Jesse Nicola -- Shredical six different ways to Sunday! -- My Portfolio

Nevermind, I found the

jnicola's picture

Nevermind, I found the solution, although it wasn't easy to dig up:

Add the following to the .htaccess file AFTER RewriteEngine on

RewriteBase /
RewriteRule ^[SUBDIRECTORY]/(.*)$ /$1 [R=301,NC,L]

Jesse Nicola -- Shredical six different ways to Sunday! -- My Portfolio

You made my day!

BarisW's picture

Thanks @jnicola for sharing this.
I've been searching for hours on strings like 'htaccess redirect directory', 'htaccess path redirect', etc.

But just before giving up i tried 'drupal subdirectory redirect' and voila, the first link on Google.

THANX!

Kind regards, Baris Wanschers

Useful Modification

wesdunn1977's picture

I was looking for the same solution and tried this code, it works to send the sub folder to root but not the url's inside, as I managed to drop by there is a good chance someone else will so I will put the slight code change that works for me to redirect all URL's in the subfolder back to root

RewriteRule ^subdirectory/.*$ / [R=301,NC,L]

The only difference to the code provided is that my code does no have () around .* and I have removed $1 after /

I'm not 100% sure on this but I think the original code is telling the re-write to preserve the URL string or structure so subdirectory/someurl will only strip subdirectory away and leave /someurl resulting in a 404 for all URL's in the subdirectory folder.

a clearer example may be yourdomain.com/subdirectory/someurl
the original code will strip subdirectory away leaving you with
yourdomain.com/someurl, my example should leave you only with yourdomain.com I believe... Hope this helps someone as the original example put me on the right track so I'm grateful :-)

Wes Dunn
Costas Online
Costas Online

That was a good find Jesse -

gonz's picture

That was a good find Jesse - using .htaccess is super key to SEO - especially when making major changes to your site architecture.

One thing to keep in mind - is not everything needs to have a 301. I have worked with many clients where we re-structured their site to be more SEO friendly and did not even bother trying to 301 every page. This would mainly be for static websites - thankfully Drupal makes it easy to 301 your whole site because it should match up to the new structure - as Drupal is pretty specific on its architecture.

Good luck with that - if you ever need any other advice - never hesitate to hit me up.