.htaccess redirect request from specific domain to another domain - apache

I have an apache server hosting a site. I have 2 domains pointing to this server, www.mysite.se and www.mysite.com.
I'm trying to figure out how to in the htaccess file to redirect traffic coming from the www.mysite.se domain to www.mysite.com/se/
I've tried several ways but cannot get it to work. I always just end up on the root of the site, as in www.mysite.com instead of the /se/ path.
This is what I've tried so far:
RewriteEngine On
RewriteCond %{http_host} ^www\.mysite\.se [NC]
RewriteRule ^(.*)$ http://www.mysite.com/se/ [R=301,NC]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mysite.se$ [NC]
RewriteRule ^(.*)$ /se/$1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mysite.se$
RewriteRule ^(/)?$ se [L]
What am I doing wrong?

This seems to have finally done it!
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mysite.se$
RewriteRule (.*)$ http://www.mysite.com/se/ [R=301,L]

Related

.htaccess redirect to another folder but keep the original domain

First,
my 'example.com' domain is linked to '/home/defaultfolder/' folder.
I want to redirect
example.com , www.example.com
to
/home/somefolder/example/ , not /home/defaultfolder/ folder
by using .htaccess. (both are inside the DocumentRoot directory)
also, subdomains like
a.example.com
to
/somefolder/example/a/
but keeps the 'example.com' domain.
I have tried some examples on the web, but nothing could have done it.
How can I write the .htaccess to do so? Thank you.
Of course, I cannot change server settings(like alias virtual hosts..) and that's why I am trying to do it by modifying the file.
I have tried
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule /somefolder/example/$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule !^/somefolder/example/ /somefolder/example/1%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^/?(?:www\.)?example.com
RewriteRule ^(.*)$ /somefolder/example/$1 [R=permanent,L]
and some others..
Have it this way in /home/.htaccess (site root):
RewriteEngine On
# handle example.com
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example)\.com$ [NC]
RewriteRule .* somefolder/%1/$0 [L]
# handle any sub.example.com
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^.]+)\.(example)\.com$ [NC]
RewriteRule .* somefolder/%2/%1/$0 [L]
I think you should modify the server setting, make the domain root folder to what you want.

Want to redirect TLD domain to a subdomain on https with htaccess. Tried htaccess RewriteRule Redirect found but not working

I want to redirect via .htaccess directives all these:
http://domain.com AND www.domain.com AND http://subdomain.domain.com
TO :
https://subdomain.domain.com
Please note that "subdomain" is an unique subdomain, I do not want to redirect any subdomain but only one.
I'm not familiar with .htaccess directives so I tried all RewriteRule or Redirect snippet found here or elsewere, but it's not working. I must miss something.
EDIT: I added a .htaccess into sur directory of subdomain with this and it works now:
It's OK, I added this in htaccess into the subdirectory of subdomain :
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Try with:
# for main domain
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ https://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]
# for sub domain
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]

Apache redirect not working

I have two domains example.com and example.net using the same .htaccess file in public_html directory. I want any valid url like example.net/valid-url to redirect to example.com/valid-url.
I tried the following:
RewriteCond %{HTTP_HOST} ^example.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.net$
rewriterule ^(.*)$ http://www.example.com/$1 [R=301,L]
However the redirect is happening (with above code) to example.com not,
example.com/valid-url.
Is there something I'm doing wrong?
Try this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.net$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L,NE]
Make sure to clear your browser cache before testing this.

Apache detected external loop redirection with target URL after .htaccess setup

I've played around with this for hours, trying to get it right. It seems to be working the way I want, but I'm getting this Apache error.
I want to set it up so if someone visits my main URL they're directed to https and www, so I have this in the htaccess file in the main public_html directory.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.com
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Next I want anyone who hits subdomain.domain.com or domain.com/subdomainname to redirect to https://subdomain.domain.com, so I have this in the .htaccess file in the subdomain folder.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.subdomain\.domain\.com$
RewriteRule ^/?$ "https\:\/\/subdomain\.domain\.com\/" [R=301,L]
Any hints as to what I'm doing wrong?
Replace your subdomain .htaccess by this:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !=subdomain.domain.com
RewriteRule ^(.*)$ https://subdomain.domain.com/$1 [R=302,NE,L]

RewriteCond and RewriteRule in .htaccess

I have a client folder located at http://www.example.com/client
However, I've now installed SSL on the server, and want to add a permanent redirect using HTACCESS so that whenever /client is accessed, that it redirects to: https://www.example.com/client
Anybody know how to do that?
I've redirected my domains in the past like this:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
This should not affect the solution, but the site must still redirect to www.example.com FIRST, and then to https://www.example.com/client if for example, http://www.example.co.za/client is entered.
Try this:
RewriteCond %{HTTPS} !on
RewriteRule ^client(/.*)?$ https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteEngine On
RewriteRule ^/?$ https://www.example.com/client [301,NC,L]
It tells the apache, whenever the url is https://www.example.com or either with slash at the end, will redirect to ur /client