htaccess redirect root to subdirectory but allow index.php in root to function

This is our situation: The original domain name (say, www.example.com) was done in WordPress and installed in the root. We have created a new website using Joomla and placed it in a sub-directory (say, www.example.com/PORTALsite).

I have been able to successfully redirect the root (www.example.com/) to this sub-directory (www.example.com/PORTALsite) using this code in htaccess:

Read More
RewriteEngine on
RewriteRule ^$ http://www.example.com/PORTALsite [R=301,L]

This works good, EXCEPT that we need the original WordPress website (the Home page located at www.example.com/index.php) to still function. We need this, because we have hundreds of pages within this WordPress site that are indexed in the Search Engines and we want all of that old content to continue to function.

The code above works well for all files and sub-directories within the Root, allowing our old content to continue to function, and it correctly redirects the root itself (www.example.com/) to the new Joomla website’s sub-directory (www.example.com/PORTALsite).

HOWEVER, the problem is this: We still need the old WordPress site’s index.php file to function if it is accessed directly (i.e., if someone types in www.example.com/index.php), BUT it is NOT functioning. If you go to www.example.com/index.php is also redirects to www.example.com/PORTALsite. Though all of the other files in the root still function as we desired them to.

Let me make sure I am explaining this properly: If someone goes our domain (www.example.com) it redirects as we want to the new Joomla website (at www.example.com/PORTALsite). BUT, if you go to www.example.com/index.php (the old WordPress site’s Home page) it also redirects to the new Joomla website’s sub-directory (www.example.com/PORTALsite). We do Not want this to happen. We want it so that if the root’s index.php file is accessed directly, that it will still function (to keep all the old WordPress content intact).

We ONLY want requests to the domain name itself, www.example.com, to redirect to www.example.com/PORTALsite. But, we need all of the other files and sub-directories, including www.example.com/index.php, to still function (so that people can still access all of the old WordPress website’s content, as well as numerous static pages).

All the files and sub-directories in the root (www.example.com/) still function as we want, BUT just the old WordPress site’s Home page (located at www.example.com/index.php) also redirects to the new site at www.example.com/PORTALsite (along with requests for the domain name itself www.example.com). So the code above is VERY close to working as we desire.

I greatly appreciate the expertise here on StackOverflow and greatly appreciate any help we can receive. Thank you in advance!

UPDATE: This is the code in our HTACCESS file: (used “example.com” rather than true domain name)

RewriteEngine on
RewriteRule ^$ http://www.example.com/PORTALsite [R=301,L]

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

# END WordPress

In light of anubhava responses below, his code should work once I get WordPress to stop removing the “index.php” from the URL. Thank you anubhava! Here’s the whole file.

Related posts

Leave a Reply

1 comment

  1. Replace this rule:

    RewriteRule ^$ http://www.example.com/PORTALsite [R=301,L]
    

    with this:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/+[s?]
    RewriteRule ^$ /PORTALsite [R=301,L]
    
    • Make sure this is first rule in your wordpress .htaccess
    • Make sure to test in a different browser to avoid 301 caching issues
    • Change permalink setting of WP to make your BLOG/Home URLs as http://domain.com/index.php instead of http://domain.com/