Ad

.htaccess Correct Way To Redirect From One Wordpress Instance To Another?

First, I am sorry if there are similar threads, but I cannot figure out what is the right approach for the following problem:

I cannot figure out, how to properly change my .htaccess files, so that the following conditions will work:

I have 2 independent WordPress instances under one domain. (Lets say: example.com)

  • Old instance is hosted here: example.com/old/
  • New instance is hosted here: example.com/new/

Current behaviour when i access example.com:

  • When I access example.com, it redirects to the old instance. Browser is showing: example.com/old

Desired behaviour, when i access example.com:

  • It Redirects to example.com/new/
  • Hides the subfolder path (/new) from the url, so that in the address bar only example.com is visible (or any other part except the /new/) in the middle.

My first approach was to add the following line in the /old/.htaccess:

Redirect 301 / https://example.com/new/

But then, it redirected example.com to: example.com/new/old/ which resulted in a 404. Since the .htaccess files look kind of identical in /old/ and /new/ I cannot figure out, where exactly is defined, that example.com/ is redirecting to /old/ by default.

Here are the .htaccess contents of each instance and root folder:

.htaccess-content / (root)

# -FrontPage-

IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>

.htaccess-content /old/ (old instance)

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/
RewriteRule .* - [CO=wordpress_test_cookie:WP+Cookie+check:%{HTTP_HOST}:1440:/]

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

# END WordPress

.htaccess-content /new/ (new instance)

#BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /new/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /new/index.php [L]
</IfModule>

# END WordPress

<IfModule mod_rewrite.c>
    RewriteEngine On
    
    RewriteCond %{HTTPS} !=on
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    Header always set Content-Security-Policy "upgrade-insecure-requests;"
</IfModule>

Any help is highly appreciated.

Thank you.

Ad

Answer

You need to use a RewriteRule in your old/. htaccess instead of using Redirect . A RewriteRule can map the request from your old directory to the new directory without a change in URL.

RewriteEngine On

RewriteCond %{REQUEST_URI} !/new [NC]
RewriteRule ^(.*)$ /new/$1 [L]

This should go in htaccess in your /root directory and remember to put this at the top or before other rewriting directives.

Also change "WordPress home URL" and "Site address" by going to settings->general in your WordPress dashboard. You can use https://example.com as the WordPress home URL and https://example.com/new as your WordPress site address.

Let me know how it works for you.

Ad
source: stackoverflow.com
Ad