htaccess: Redirect entire subfolder to root

2019-01-25 00:24发布

问题:

My site uses Wordpress as a CMS and it's installed in a subfolder already but I set it up to display on the root domain. Basically, example.com displays the contents of example.com/wordpress.

The old (static) website was also in a subfolder and I need to redirect that entire subfolder (folder and all contents) back to the root - e.g. point example.com/oldwebsite to example.com.

I've tried lots of different approaches and I just can't get it to work.

回答1:

The following solution is quite simple to implement. What you get is:

  • wordpress installation in a folder in the root, like in /wp
  • wp admin is accessible via http://www.yourdomain.test/wp/wp-admin/
  • site opens in root folder like: http://www.yourdomain.test/

Remarks

  • if you don't use permalinks, you may not need the .htaccess remarks; I always use permalinks, and I think everybody should do so, and then it might not work if you don't edit the .htaccess (I'm not sure)
  • if you don't know what you're doing, and you're afraid to mess things up, tar/zip the wordpress folder before you start
  • you may want to use the plugin backwpup to create a backup of the database before you start
  • if you have the option of a snapshot of your server, take one now

What you need to get this working:

  • if you want permalinks, get that working first; this creates or needs a .htaccess file
  • copy /wp/index.php to /index.php, so to the root of your site
  • if you have a .htaccess, copy that to the root as well
  • if you have a .htaccess in the root already, you need to combine them somehow; I'll suppose for now that this is not the case

Edit /index.php

  • add or edit the following line, which is probably the last line of this file
  • you need to add the subdirectory to your wordpress installation to the path

    require('./wp/wp-blog-header.php');
    

Edit /.htaccess

  • add the following code to the end of the .htaccess or create a new .htaccess
  • replace /wp/ with the name of the directory where you installed wordpress

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /wp/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /wp/index.php [L]
    </IfModule>
    # END WordPress
    

Wordpress settings

  • open the wordpress admin, and go to settings > general
  • edit the following two fields
  • wordpress address: http://www.yourdomain.test/wp
  • site address: http://www.yourdomain.test/
  • the wordpress address is for links to stylesheets and images, the site address is where links to other pages should go to

Renaming the wordpress directory

  • if you rename the wordpress folder, do this in the site admin first
  • this updates the database value
  • after the update, the admin won't work anymore
  • then rename the directory name in the terminal or in your ftp-client
  • then check all .htaccess and index.php files for the old name, and replace the old name with the new one


回答2:

This is a case study of which I recently moved from a sub-directory (/wordpress) to the root directory (/).

You can get some interesting tips by reading this or better still, assign the move to us at a nominal cost and save yourself all the hair-pulling :)

Step 1: The site was on Cpanel, so I took a full backup of the site to avoid chances of a major disaster happening if something went wrong (you know the Murphy's laws, don't you?).

Step 2: I upgraded the current blog (at /wordpress) to the latest version so that the database structure will be up to date.

Step 3: I downloaded all the files in the root directory to a local folder so that I can replace the overwritten files, if any are required.

Step 4: I then uploaded a full copy of the blog software to the root directory (public_html folder as this was a cpanel site). If there are any special plugins used, you should download the software from the previous location and then upload this in the new location so that the plugin files are also uploaded.

Step 5: I uploaded the wp-config.php fie in the root directory so that Wordpress will refer to the same database.

Step 6: I accessed the database through PHPmyadmin and opened the wp_options table. Updated the values of the site_url and home options to http://www.domain.com by replacing http://www.domain.com/wordpress.

Step 7: Now I accessed the blog through the root URL and it opened fine. However I noticed that the posts were still being linked to www.domain.com/wordpress/postname/id.

Step 8: I accessed the admin console and recreated the Permalinks so as to update all the post URLs.

Now the whole blog was working fine without any issues.

Lastly, I needed to make sure that Google and other search engine would redirect the old indexed /wordpress/post URLs to the new ones. For this, I edited the .htaccess file in the root directory.

Inserted the following code before the # BEGIN WORDPRESS section (if you don't keep this code out of the WORDPRESS section, Wordpress will remove this when you update Permalinks or do any other act that works with .htaccess file).

RewriteEngine On RewriteBase / rewriterule ^wordpress(/.*)? $1 [L,R=301]

This effectively creates a permanent redirect for all files with /wordpress/something name to /something names.