When I set up my WP website, instead of installing it directly into public_html I installed it in public_html/my_subdir. For this to work I made the following .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/my_subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ my_subdir/index.php [L]
</IfModule>
Then, I wanted to redirect www.example.com to www.example.com/dev so I added a 301 redirect from Cpanel. The htaccess above created a redirect rule in Cpanel, and it was conflicting with my new redirect, so I erased the first one and my htaccess ended up as:
<IfModule mod_rewrite.c>
RewriteEngine on
</IfModule>
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^/?$ "https://www.example.com/dev/" [R=301,L]
It works, if I go to www.example.com it takes me to www.example.com/dev but if I go to www.example.com/services it takes me to a Error 404 page.
How can I make it so going to any example.com page takes me to its equivalent in example.com/dev like example.com/services = example.com/dev/services
2
Answers
Ok, I just realized the solution is more simple than I could ever imagine:
I wrote the answer within my question the whole time. Just for future people doing the same: I have 2 websites (inside two different folders) within my public_html folder (because I'm a freak with keeping things organized):
my_subdir (my old website)
my_subdir_2 (new website, under construction)
I wanted both sites to be working under the same domain and avoid the whole back up, change WP domain data, back up, move over to new domain, etc etc etc.
So, I set things up so www.example.com would redirect to www.example.com/my_subdir (my old website) and my new website to be accesible at www.example.com/my_subdir_2 (so I can continue working on it.
The code above makes it work like that, and you can have WP's general domain set up as example.com for both sites.
After I finish my new website, I will just (backup) and delete my old website's folder and change the htaccess into:
This only redirects the document root (
^/?$
) to/dev/
. To redirect/<anything>
to/dev/<anything>
you need to change the directive to something like the following:(All those backslash escapes in the
RewriteRule
substitution are not required – that is quite typical of cPanel.)However, this will result in a redirect loop, since it will also redirect
/dev/<anything>
itself. So, you can change the above to read:Test with 302 (temporary) redirect first and only change to 301 (permanent) when you are sure it’s working as intended – to avoid potential caching issues.