Using .htaccess to rewrite to WordPress subdirectory?

As opposed to many questions here in StackOverflow, I’m looking for something a little different. I have a WordPress install in a subfolder of my domain called “blog”. The main part of the website is a Magento website. I’m looking to take any instance where “blog” is part of the URI, and make sure it’s untouched by the myriad of other RewriteRules in Apache.

As a few examples:

Read More

http://www.example.com/blog/wp-admin/

http://www.example.com/blog/

http://www.example.com/blog/save-money-groceries-without-coupons/

…should all direct to the WordPress site found under /blog/

http://www.example.com/

http://www.example.com/plumbing/faucets.html

http://www.example.com/about/family-history.html

…should all direct to the Magento site that is currently found under the root directory

What is the best practice in writing the .htaccess file to achieve this result?

Related posts

Leave a Reply

2 comments

  1. To redirect http://www.example.com/blog/wp-admin/ to http://www.blog.example.com/wp-admin/ you can add the following to the .htaccess file

    RewriteEngine On
    RewriteRule ^blog/(.*)$ http://www.blog.example.com/$1 [L,R=301]
    
  2. I’m not an expert on setting up Apache servers but I think you want your WordPress install in a completely different folder to Magento and then use Alias in your .conf file. You can read about the Alias directive here.

    For example in my Apache .conf files I have

    Alias /blog/ "/full/path/to/wordpress/htdocs/"
    

    This way the folder /full/path/to/wordpress/htdocs/ has its own .htaccess and its own index.php – which is what you are going to want for running WordPress without jumping through hoops.

    Remember to restart Apache if you update the .conf file.