Redirecting add-on subdomain away from main domain

shrubby.burrow_0v

Registered
Mar 4, 2023
2
0
1
US
cPanel Access Level
Website Owner
Let's say I have 3 domains:

maindomain.com
addon1.com
addon2.com

My host does not allow me to move addon domains outside of the main domain's public_html folder.

So my addon1.com domain is available via:
https://addon1.com, https://addon1.com.maindomain.com, and https://maindomain.com/addon1.com

How do I create a rewrite rule that will redirect:
1. https://addon1.com.maindomain.com and any optional trailing files/folder structure in the request to https://addon1.com
2. https://maindomain.com/addon1.com and any optional trailing files/folder structure in theh request to https://addon1.com

RewriteCond %{HTTP_HOST} ^addon1\.com\.maindomain\.com(.*)$ [OR]
RewriteCond %{HTTP_HOST} ^www\.addon1\.com\.maindomain\.com(.*)$
RewriteRule ^(.*)$ "https\:\/\/addon1\.com\/$1" [R=301,L]

This works for part 1 above, but not for any optional trailing files/folder structure. And I'm very unclear how to do part 2.
 

Alan_Scott

Member
Oct 28, 2022
5
2
3
Wolverine
cPanel Access Level
Website Owner
Let's say I have 3 domains:

maindomain.com
addon1.com
addon2.com

My host does not allow me to move addon domains outside of the main domain's public_html folder.

So my addon1.com domain is available via:
https://addon1.com, https://addon1.com.maindomain.com, and https://maindomain.com/addon1.com wordle

How do I create a rewrite rule that will redirect:
1. https://addon1.com.maindomain.com and any optional trailing files/folder structure in the request to https://addon1.com
2. https://maindomain.com/addon1.com and any optional trailing files/folder structure in theh request to https://addon1.com

Code:
 RewriteCond %{HTTP_HOST} ^addon1\.com\.maindomain\.com(.*)$ [OR]
RewriteCond %{HTTP_HOST} ^www\.addon1\.com\.maindomain\.com(.*)$
RewriteRule ^(.*)$ "https\:\/\/addon1\.com\/$1" [R=301,L]
This works for part 1 above, but not for any optional trailing files/folder structure. And I'm very unclear how to do part 2.

To redirect both https://addon1.com.maindomain.com and https://maindomain.com/addon1.com to https://addon1.com with optional trailing files/folder structure, you can use the following rewrite rule in your .htaccess file:

Code:
RewriteCond %{HTTP_HOST} ^(www\.)?addon1\.com\.maindomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?maindomain\.com$
RewriteCond %{REQUEST_URI} !^/addon1\.com/
RewriteRule ^(.*)$ https://addon1.com/$1 [L,R=301]
This rule first checks if the request is coming from either addon1.com.maindomain.com or maindomain.com. If it is, it then checks if the request does not already contain "/addon1.com/" in the URL. If it doesn't, then it redirects to https://addon1.com with the requested URI appended to it.

Make sure to replace "addon1.com" with "addon2.com" and "maindomain.com/addon1.com" with "maindomain.com/addon2.com" respectively in the above rule for addon2.com domain.
 

shrubby.burrow_0v

Registered
Mar 4, 2023
2
0
1
US
cPanel Access Level
Website Owner
To redirect both https://addon1.com.maindomain.com and https://maindomain.com/addon1.com to https://addon1.com with optional trailing files/folder structure, you can use the following rewrite rule in your .htaccess file:

Code:
RewriteCond %{HTTP_HOST} ^(www\.)?addon1\.com\.maindomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?maindomain\.com$
RewriteCond %{REQUEST_URI} !^/addon1\.com/
RewriteRule ^(.*)$ https://addon1.com/$1 [L,R=301]
Unfortunately this does not appear to work.

This will redirect addon1.com.maindomain.com to addon1.com
This will not redirect addon1.com.maindomain.com/blah/ to addon1.com OR
This will not redirect addon1.com.maindomain.com/blah/ to addon1.com/blah/
This will not redirect maindomain.com/addon1.com to addon1.com
This will not redirect maindomain.com/addon1.com/blah/ to addon1.com OR
This will not redirect maindomain.com/addon1.com/blah/ to addon1.com/blah/
 

Alan_Scott

Member
Oct 28, 2022
5
2
3
Wolverine
cPanel Access Level
Website Owner
Unfortunately this does not appear to work. wordle unlimited

This will redirect addon1.com.maindomain.com to addon1.com
This will not redirect addon1.com.maindomain.com/blah/ to addon1.com OR
This will not redirect addon1.com.maindomain.com/blah/ to addon1.com/blah/
This will not redirect maindomain.com/addon1.com to addon1.com
This will not redirect maindomain.com/addon1.com/blah/ to addon1.com OR
This will not redirect maindomain.com/addon1.com/blah/ to addon1.com/blah/
[/SIZE]
I apologize for the confusion earlier. Here is an updated .htaccess code that should work for your requirements:

apache
Code:
RewriteEngine On

# Redirect addon1.com.maindomain.com to addon1.com
RewriteCond %{HTTP_HOST} ^(www\.)?addon1\.com\.maindomain\.com$ [NC]
RewriteRule ^(.*)$ https://addon1.com/$1 [L,R=301]

# Redirect maindomain.com/addon1.com to addon1.com
RewriteCond %{HTTP_HOST} ^(www\.)?maindomain\.com$ [NC]
RewriteRule ^addon1\.com/?(.*)$ https://addon1.com/$1 [L,R=301]
This code should now provide the correct redirects:
Code:
- addon1.com.maindomain.com to addon1.com
- addon1.com.maindomain.com/blah/ to addon1.com/blah/
- maindomain.com/addon1.com` to `addon1.com`
- maindomain.com/addon1.com/blah/ to addon1.com/blah/
Please make sure you add these rules at the beginning of your .htaccess file and test them thoroughly. Let me know if you have any issues or further questions!
 
  • Like
Reactions: cPRex