htaccess redirect with www and non www - .htaccess

I've searched all over and I can find ways to direct www to non and the other route as well. But it feels like I have a bit of a different case.
I am trying to use a vanity URL to direct users to an alternate url and path. So that when a user enters www.vanityurl.com or vanityurl.com it directs them to domain.com/path/id
This is what I have
RewriteCond %{HTTP_HOST} ^test.com [OR]
RewriteCond %{HTTP_HOST} ^www.test.com [NC]
RewriteRule ^(.*)$ https://www.domain.com/path/id/$1 [L,R=301,NC]
This works great for www. But, test.com either fails or redirects to just domain.com with out the path.
Help!?

Related

Redirect Specific Wildcard Subdomain to a specific url of another domain

I want to redirect specific wildcard subdomain to a specific url of another domain. the both domains are on the same server/public_html, and redirect all the inner pages of the wildcard subdomain to the inner pages of the other domain.
e.g
redirect music.example.com to stackoverflow.com/music/
and
redirect music.example.com/happy-birthday/ to stackoverflow.com/happy-birthday
redirect music.example.com/sing-to-you/ to stackexchange.com/sing-to-you/
I have more than 100k post so i cannot make the redirection of the inner pages one by one using .htaccess
Please help me out,
I tried the below code
RewriteCond %{HTTP_HOST} ^(music.example.com) [NC]
RewriteRule ^()$ https://www.stackoverflow.com/music/ [R=301,L]
but it only redirect the homepage to the specific url which I wanted, but the rest of the urls is the main issue, If I use the normal domain redirection, it works for the rest of the urls, but the function of the code above will stop working
Here is the below code for normal domain redirect redirection
RewriteCond %{HTTP_HOST} ^music\.domain.com\.com [NC]
RewriteRule ^(.*)$ https://wwww.stackoverflow.com/$1 [L,R=301,NC]
I used the below htaccess code and it works perfectly.
RewriteEngine On
# "music.example.com/" to "another.example/music/"
RewriteCond %{HTTP_HOST} ^music\.example\.com [NC]
RewriteRule ^$ https://another.example/music/ [R=302,L]
# "music.example.com/<something>" to "another.example/<something>"
RewriteCond %{HTTP_HOST} ^music\.example\.com [NC]
RewriteRule (.+) https://another.example/$1 [R=302,L]
The RewriteCond (condition) directive checks the requested host. The RewriteRule naturally redirects to the other domain. In the second rule the URL-path from the request is captured in the $1 backreference.

Conditional htaccess redirect

Is it possible to make a conditional redirect using .htaccess in a way that if somebody access my domain directly (example.com) he will be redirected to a subdomain (subdomain.example.com) or may be to another domain, but if someone accesses it through a particular URL (example.com/magic) he get's redirected to example.com i.e. main domain.
In other words, I wanted my users to access the main domain (example.com) only if they are using example.com/magic.
My htaccess is presently redirecting all users of main domain to a subdomain.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/sub\.example\.com" [R=301, L]
I couldn't find though, how to put the condition in my htaccess.
You may use this rule to redirect every URI that is not /magic or /magic/ to a sub-domain:
RewriteEngine On
# redirect /magic
RewriteRule ^magic/?$ / [L,NC,R=301]
# redirect anything except /magic
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule !^(magic/?)?$ https://sub.example.com [R=301,L,NE]

Redirect subdomain/subfolder to root domain/subfolder - But only in SOME instances

When I set up my subdomain, I created some links incorrectly. Now Google thinks I have some pages on both my subdomain and my root domain. I need to fix this, but I can't redirect the entire subdomain.
Examples of what I want to do:
https://sub.example.com/ (no redirect)
https://sub.example.com/keep-1 (no redirect)
https://sub.example.com/keep-2 (no redirect)
https://sub.example.com/move-1/* => https://example.com/move-1/*
https://sub.example.com/move-2/* => https://example.com/move-2/*
I've tried a number of .htaccess solutions and I'm close, but I can't figure it out. Here's what I've tried:
Attempt #1 - Correctly redirects, but doesn't work as a solution because it redirects everything from the subdomain
RewriteCond %{HTTP_HOST} ^sub\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301,NE]
Attempt #2 - Doesn't redirect anything - Seems like the right solution but I'm missing something about how redirects work, I think...
RewriteCond %{HTTP_HOST} ^sub\.example\.com/move-1/ [NC]
RewriteRule ^(.*)$ https://example.com/move-1/$1 [L,R=301,NE]
Attempt #3 - Doesn't redirect anything
RewriteCond %{HTTP_HOST} ^sub\.example\.com/move-1/(.*)$ [NC]
RewriteRule https://example.com/move-1/$1 [L,R=301,NE]
Attempt #4 - Doesn't redirect anything
RewriteBase /
RewriteRule ^sub\.example\.com/move-1/(.*)$ https://example.com/move-1/$1 [R=301]
My .htaccess file is in the root domain's root html folder and seems to have control. I have also tried these from the subdomain's root folder, but that didn't redirect anything.
RewriteCond %{HTTP_HOST} ^sub\.example\.com$
RewriteRule ^move(.*) https://example.com/move$1 [R=301,L]
%{HTTP_HOST} is the host name, mapped to domain such as sub.example.com or example.com. It does not contain any path part, that follows behind the domain. $1 is back-reference, mapped to the regex part (.*). The RewriteRule tells if the request uri pattern starts with /move, then redirect to https://example.com/move$1 permanently.

htaccess subdomain redirection at multiple levels

I have this rule in my htaccess:
RewriteCond %{HTTP_HOST} !^(www|cdn)\.domain\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.domain\.com$
RewriteRule ^(.*)$ user/%1/$1
right now http://username.domain.com/ works good, internally redirects to http://domain.com/user/username.
My goal is to redirect everything inside "user" folder, obtaining something like this http://username.domain.com/subfolder2/sybfolder3/ that redirects internally to http://domain.com/user/username/subfolder2/subfolder3/

How can I create dynamic subdomains for dynamic domains

In the app I'm working at the moment, I need to dynamically create subdomains. That would be quite trivial if I knew what was the domain I need to redirect to.
The point is that I can have many domains pointing to the same server and each may have many subdomains and I want a htaccess that can handle it.
For example: domain1.com, domain1.com.br, domain2.com, domain2.co.uk can all be pointing to my server. When a user tries to access sub.domain1.com, I want it to be transparently redirected (no 301 redirect) to domain1.com. If he tries to access sub.domain1.com.br, I want it to be transparently redirected to domain1.com.br and so on (note that both redirects would point to the exact same point, the only difference would be in the address bar).
By now, I tried unsuccessfully to make changes in the lines that redirect from www. to non-www domains, but nothing worked as expected:
//This works
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
//This ends with a "too many redirections" error
#RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
#RewriteRule ^(.*)$ http://%1/$1 [L,QSA]
Can someone help?
Try this:
RewriteCond %{HTTP_HOST} ^(.+)\.([^.]{4,})\.([a-z]{3,4}|[a-z]{2,3}\.[a-z]{2}|[a-z]{2})$ [NC]
RewriteRule ^(.*)$ http://%2.%3/$1 [L,R=301]
This should cover all of these:
domain1.com
domain1.com.br
domain2.co.uk
domain3.es
domain4.info

Resources