htaccess rewrite all subdomains to url path - .htaccess

i need to redirect all request going to any subdomain on domain1.com to domain2.com passing the subdomain part as path, so for example:
sub1.domain1.com -> domain2.com/sub1
this works with my current rewrite rule:
RewriteCond %{HTTP_HOST} ^.+?\.domain1\.com$ [NC]
RewriteRule ^(.*)$ https://domain2.com/$1 [L,R=301]
but when i have a subdomain like the following, it doesn't work:
sub1.sub2.domain1.com -> is redirecting to domain2.com/ without taking the subdomain parts to the path. (domain2.com/sub1.sub2)
how can i get this to work for any combination of subdomain?

I fail to see who any such redirection currently works. The rewriting rule you posted certainly does not do what you describe...
I suggest such an approach to hand over the "subdomain name":
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:(.+)\.)?domain1\.com$ [NC]
RewriteRule ^ https://domain2.com/%1 [L,R=301]
https://sub1.sub2.domain1.com/some/path => https://domain2.com/sub1.sub2
This obviously will ignore whatever path has originally been requested, but that is what you intend, as I understood. If you also want to keep the path and only precede it with the subdomain name, then this variant should point you into the right direction:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:(.+)\.)?domain1\.com$ [NC]
RewriteRule ^/?(.*)$ https://domain2.com/%1/$1 [L,R=301]
https://sub1.sub2.domain1.com/some/path => https://domain2.com/sub1.sub2/some/path
It is a good idea to start out using a 302 redirection and only change that to a 301 once things work as desired. That prevents you from running into caching issues...

Related

Redirect sub page to www in htaccess

I have a multi site where the sub pages should redirect from non-www to www.
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^/?$ "https\:\/\/www.example\.com" [R=301,L]
This code above works if you enter https://example.com, then you are forwarded to https://www.example.com. But if you go directly to a subpage such as https://example.com/about, then you aren't redirected to https://www.example.com/about.
How do I write the redirect to fix this?
I have tried different redirects but I can't get them to work.
Well, i your rule you explicitly only match requests to the root path (/) by your pattern ^/?$. That pattern will obviously only match an empty string or a string with a single slash in it and nothing else.
So you need to change that such that the rules gets applied to all requests and that the requested path is preserved in the rewriting step:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^ https://www.example\.com%{REQUEST_URI} [R=301,L]
An alternative you often see is the following. It does not really make sense in your specific situation where you want to rewrite all requests to the "www" variant of your domain. But it might allow for more flexibility if you need to tell apart different requests for specific purposes (like requests to an API versus requests for the UI):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^/?(.*)$ https://www.example\.com/$1 [R=301,L]

re-direct a domain to a sub-domain + make it https

this is kinda an odd one:
I need my site to do two things (one of which is already working):
if a user tried to access the domain via HTTP:// it is replaced with https:// - this is for SEO in google and to make the user feel more secure -
the site folder that is used to load the website needs to be the subdomain folder of the site
Oddly the second part of this is working and I figured out - however I'm not sure how to merge these two requests:
HTACCSESS
RewriteEngine on
RewriteCond %{HTTP_HOST} ^trippy\.co\.nz$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.trippy\.co\.nz$
RewriteCond %{REQUEST_URI} !update.trippy.co.nz/
RewriteRule (.*) /update.trippy.co.nz/$1 [L]
But I'm not sure how to make the site display as
https://trippy.co.nz/
I have tried:
RewriteEngine On
RewriteCond %{HTTP_HOST} update\.trippy\.co\.nz [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://update.trippy.co.nz/$1 [R,L]
but then the web address displays as: https://update.trippy.co.nz
and I need to remain as https://trippy.co.nz/
Any help here would really great and I know its a odd situation to be in.
THanks,
Wally
...but then the web address displays as: https://update.trippy.co.nz
You would seem to be redirecting to the subdomain itself, not the subdomain's subdirectory, as you appear to be doing in the first rule. You may also be putting the directives in the wrong order - the external redirect needs to go first - otherwise you are going to expose the subdomain's subdirectory, which does not appear to be the intention.
Try the following instead:
RewriteEngine On
# Redirect HTTP to HTTPS
RewriteCond %{HTTP_HOST} ^(www\.)?trippy\.co\.nz [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]
# Rewrite all requests to the subdomain's subdirectory
RewriteCond %{HTTP_HOST} ^(www\.)?trippy\.co\.nz [NC]
RewriteRule !^update\.trippy\.co\.nz/ /update.trippy.co.nz%{REQUEST_URI} [L]
No need for the extra condition in the 2nd rule block, as the check can be performed directly in the RewriteRule and use the REQUEST_URI server variable instead of the $1 backreference in the substitution string.
That that R by itself is a temporary (302) redirect. You may want to change that to R=301 (permanent) once you have confirmed this is working OK.

htaccess rewrite - redirect to another subdomain is looping

I'm trying to redirect two kind of urls to a subdomain and all the others to my main domain.
A concrete example :
"my account" pages starting by /my-account/* and subscription page must be redirected to https://my-account.domaim.com. The uri must be kept.
all others like /news or the homepage must be seen on www.domain.com only
Here is what I have tried until now :
# All urls except my-account/* and subscription are redirected to the main domain
RewriteCond %{HTTP_HOST} ^my-account\.domain\.(.+)$
RewriteCond %{REQUEST_URI} !^my-account/
RewriteCond %{REQUEST_URI} !^subscription$
RewriteRule ^(.*)$ http://www.domain.%1/$1 [L,QSA,R=301]
# subscription page is redirected to my-account subdomain
RewriteCond %{HTTP_HOST} ^www\.domain\.(.+)$
RewriteRule ^subscription$ https://my-account.domain.%1/subscription[L,QSA,R=301]
# All my-account/* pages are redirected to my-account subdomain
RewriteCond %{HTTP_HOST} ^www\.domain\.(.+)$
RewriteRule ^my-account/(.*)$ https://my-account.domain.%1/my-account/$1 [L,QSA,R=301]
Each rules work independently but if i try all of them together i'm stuck in an infinite loop.
Is anyone have an idea how to prevent it ?
The rules look fine, but you have a missing space in your 2nd rule:
RewriteRule ^subscription$ https://my-account.domain.%1/subscription[L,QSA,R=301]
# -----------------------------------------------------------------^
But you can probably combine them into a single rule:
RewriteCond %{HTTP_HOST} ^www\.domain\.(.+)$
RewriteRule ^(subscription|my-account/.*)$ https://my-account.domain.%1/$1 [L,QSA,R=301]
Also make sure you're clearing your cache when you test, as 301 redirects are permanent and the browser will cache them.
Thanks for your answer !
The typos was from my copy/paste and the combination works but doesn't change anything to the problem with the other rules. I keep it for later ;)
I tried a reversed rules like this :
#RewriteCond %{HTTP_HOST} !^my-account\.domain\.(.+)$ [NC]
#RewriteCond %{REQUEST_URI} ^/subscription$ [OR]
#RewriteCond %{REQUEST_URI} ^/my-account/ [NC]
#RewriteRule ^(.*)$ https://my-account.domain.%1/$1 [L,R=301]
It also works but not in combination with the other. it doesn't loop anymore but if i try something like http/www.domain.com/subscription i'm redirected to www.domain.com with the url truncated. It seems that the Rewrite conditions aren't correctly recognized but still can't find why ...

How to .htaccess redirect domain to subdomain while keeping the main domain intact

I want to redirect all urls under my domain
domain.com/urls, www.domain.com/profile/user1.html etc. to subdomain.domain.com/urls and subdomain.domain.com/profile/user1.html etc.
but I dont want to redirect domain.com and www.domain.com to subdomain.domain.com
Is it possible via htaccess?
Edit: I want to redirect just the internal pages and files only. But leaving the main domain.com intact.
More Examples
domain.com/page1.html to subdomain.domain.com/page1.html
www.domain.com/members/admin.html to subdomain.domain.com/members/admin.html
www.domain.com to www.domain.com (no redirection here)
Try:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/.+
RewriteCond %{REQUEST_URI} !^/index\.(php|htm)
RewriteCond %{HTTP_HOST} !^subdomain.domain.com$ [NC]
RewriteRule ^ http://subdomain.domain.com%{REQUEST_URI} [L,R]
If you want to permanently redirect, change the square brackets to: [L,R=301].
You can first handle the main page by not rewriting and redirect everything else with a second rule
RewriteRule ^$ - [L]
RewriteCond %{HTTP_HOST} !^subdomain.domain.com$
RewriteRule ^.+$ http://subdomain.domain.com/$0 [R,L]
When everything works as you expect, you can change R to R=301.

.htaccess rewrite url to main domain from subdomain

I have a domain like:
this.that.com
(it can also be accessed from:)
that.com/this
What I would like to do is make it so that the server transfers all requests from the two above url's to:
this.com
I need it so if I type in this.that.com
it will auto transfer to this.com
thanks in advance.
To redirect all requests to this.com, match everything, which is not already this.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^this\.com$
RewriteRule .* http://this.com/$0 [R,L]
If you want to redirect to the main page instead, leave out the URL path
RewriteEngine On
RewriteCond %{HTTP_HOST} !^this\.com$
RewriteRule .* http://this.com/ [R,L]
If everything works as expected, you can switch to R=301 redirection.

Resources