0

I have a WP 4.0 site that has been installed at:

mydomain.com/site

It's been there for many years, so there are lots of links from around the web pointing to it.

As part of an overhaul I am doing, I would like the wordpress site to be accessible at the root URI: mydomain.com (and www.mydomain.com), without "/site" needing to be on there.

I found articles that told me about copying the index.php and .htaccess to the root, and changing the site URI in the wordpress settings. But the instructions I've come across always seem incomplete, as they seem to assume it's a new site with no existing links to deal with in order to avoid lots of 404 errors.

So the issue I now have is that the old links generate 404 errors (because they have "/site/" in them. Images also fail to display, as they are obviously stored in the DB with "/site/" in their URI.

I have been trying to figure out a failsafe way to use .htaccess rewrite rules to redirect everything (except, wp-admin, I suppose?) from /site to /

In case it's helpful, my existing permalink structure is mydomain.com/site/%postname%-%post_id%/

Any suggestions on the best way to go about this would be greatly appreciated. Thanks.

  • Do you plan to keep the permalink structure that way? Are you only moving WordPress or also hosts – do you stick with Apache? – Sven Oct 5 '14 at 21:37
  • Yes, permalink structure will stay the same, although without the "/site/" in front of it. Same host, etc. Just wanting to drop the "/site/" out of the picture for the end user. I have other apps installed in various other subdirectories, so it is important that they don't get redirect (for instance, mydomain.com/owl/ needs to work as per normal. – omega33 Oct 5 '14 at 21:45
  • I am not sure if this is 100% right (probably not), but something along the lines of RewriteRule ^site/ /$1 [R=301,NC,L] in your .htaccess should work. All requests containing site/ will be redirected to the same URL without site in it – permanently, without considering case and without following any other rules after that. – Sven Oct 5 '14 at 22:02
  • @Sven: Thanks. I seem to be having an issue getting that to work. I am doing this on a local test server, before trying on the live one. Should I put that RewriteRule into the .htaccess of /site/ or the root, or both? If either case, is it necessary to have RewriteBase, and if so, should that be / or /site/? Is there a simple way to test if mod_rewrite is even firing? – omega33 Oct 5 '14 at 22:35
  • I tested your rewrite rule at htaccess.madewithlove.be, and it seems to behave as it should. But no joy on the test server or live site. Am going to ask the host if there is something I am overlooking. – omega33 Oct 5 '14 at 22:46
2

The only way I managed to get this to work was with the following in the .htaccess of the /site/ directory:

RewriteBase \
RedirectMatch 301 ^/site/$ /$1
RedirectMatch 301 ^/site/(?!wp-admin/|wp-content/|wp-includes/)(.*)$ /$1

Whilst I am aware that RewriteRule ^site/ /$1 [R=301,NC,L] should have worked, and that RewriteRule ^site/(.*)$ /$1 [R=301,NC,L] should have also worked (according to various articles I read), I had no luck with these (and no server error).

My host is Hostgator.com - in case this result is relevant to their particular configuration (which I doubt).

1

You can do it by moving the index.php to the root open up the index file change the following:

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/site/wp-blog-header.php' )

Replace site with the folder. Then in the admin under settings - general change the site URL and remove the folder www.website.co.uk/site will be www.website.co.uk

Then save the site will work normally though you will need to delete the .htaccess and resave the permalinks to make sure it follows the new structure.

  • Thanks Phoenix. I suspect you misunderstood my questions. I apologise if it was not clear enough. Changing the site location in the way you have suggested, was what I'd done already. My issue was that this method does not take existing links (and subsequent 404 errors) into account. I needed a way to deal with 400+ posts suddenly having a 404 error. See my answer below for how I resolved that. Cheers... – omega33 Oct 8 '14 at 5:01
  • That 404 will only happen if you used the old .htacess if you refresh it when you enter www.website.com/sites/page2 it would redirect to www.website.com/page2 automatically as really you havent moved the site just rewriting the url.. – Phoenix WS Oct 8 '14 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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