Redirect all pages except root

Mr Lucky

Well-known member
Does anyone know how I redirect all pages to another domain root, except the domain root itself?

https://domain1.com stays at https://domain1.com
https://domain1.com/all pages >>> domain2.com (root)


Thanks
 
Does anyone know how I redirect all pages to another domain root, except the domain root itself?

https://domain1.com stays at https://domain1.com
https://domain1.com/all pages >>> domain2.com (root)


Thanks

Are you using Cloudflare?
Can you use .htaccess redirects?

This should preserve the path - just swap the domain:

Code:
# Enable the Rewrite Engine
RewriteEngine On

# Condition: If the request is NOT for the root domain
RewriteCond %{REQUEST_URI} !^/$

# Redirect all requests while preserving the path
RewriteRule ^(.*)$ https://domain2.com/$1 [R=302,L]

This should redirect straight to root:

Code:
# Enable the Rewrite Engine
RewriteEngine On

# Condition: If the request is NOT for the root domain
RewriteCond %{REQUEST_URI} !^/$

# Redirect all requests without preserving the path
RewriteRule ^(.*)$ https://domain2.com/ [R=302,L]

Change "302" to "301" once you confirm it works - if you wish to keep it permanent.

A nice rule tester:

https://htaccess.madewithlove.com/
 
Last edited:
Many thanks that's perfect!

No problem.

For anyone reading this in the future: those are intended for .htacces.

I haven't played much with Cloudflare "Redirects rules" until today (they are sunsetting the "Page rules" which I used to use). Maybe it can be done via CF too, before any request even reaches the hosting server - if CF is your cup of tea. :) But this .htaccess should work, and quite efficiently too.

Relja
 
Back
Top Bottom