Redirect non-www to www excluding subdomain - .htaccess

First of all, please don't mark this question as duplicate because I've tried all other answers and no one works for me. I have a subdomain called account.domain.com and this is the code in my .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I want to redirect visits from domain.com to www.domain.com, this works fine. The problem comes when I access the subdomain account.subdomain.com and it redirects to www.account.subdomain.com/account/.php... I don't know why.
I've tested a lot of different codes but I get the same result. My site is hosted in a free account in Hostinger.es.

If you want to target only main domain then use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

Related

Redirect a domain alias a primary domain page?

We have a domain alias which we would like to redirect to the main primary domain which is working all good and well, but how do we redirect the domain alias to a page on the main website.
I have the following code in the .htaccess added (as below) but it is redirecting to the homepage which also breaks the actual page we want to redirect too.
RewriteCond %{HTTP_HOST} ^(www\.)?domainalias\.co.uk$ [NC]
RewriteCond %{THE_REQUEST} !/pirate-golf [NC]
RewriteRule ^(.*)$ /pirate-golf [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?maindomain\.co.uk$ [NC]
RewriteRule ^pirate-golf$ https://www.domainalias.co.uk [L,R=301,NE]
We want the domain alias to redirect to the main domain page like the following example: www.domainalias.co.uk to redirect to www.maindomain.co.uk/pirate-golf.
Any help on this would be great, thanks.
With your shown samples/attempts, please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.domainalias\.co\.uk$ [NC]
RewriteRule ^ http://maindomain.co.uk%{REQUEST_URI} [NE,R=301,L]
Make sure these rules are at the top of your htaccess rules file.

htaccess HTTPS redirect excluding HTTPS subdomains

I know it has been asked many times here, but can't find the right configuration for my setup.
Requirements:
http://www.example.com & https://www.example.com (& example.com) have to redirect to https://test.example.com . (Notice it is a subdomain)
https://sub_1.example.com is to be EXCLUDED and not redirected. (Notice the subdomain has an 'underscore')
What I Have:
RewriteEngine On
RewriteBase /
# EXCLUDE following sub-domains.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^sub_1\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^test\.example\.com$ [NC]
# REDIRECT to
RewriteRule (.*) https://test.example.com/$1 [R=301,L]
>> ISSUE:
My https://sub_1.example.com is in fact an API that feeds a mobile app. When I use the above code, the mobile fails. So it seems that the sub-domain is not properly excluded and protected from the re-direct.
Would appreciate some help in cleaning it up.
FIXED !!
Needed to add an exception before the existing rules, since I needed to exclude https://sub_1.example.com/api
RewriteCond %{HTTP_HOST} ^sub_1\.example\.com [NC]
RewriteRule ^api - [L]

Htaccess rewrite rules to redirect multiple domains

I'm trying to redirect some domains correctly using the htaccess file. Here is what I got:
RewriteCond %{HTTP_HOST} ^special.com$
RewriteRule (.*) https://website.com/some-special-page [L,R=301]
RewriteCond %{HTTP_HOST} !^website.com$
RewriteRule (.*) https://website.com/$1 [L,R=301]
Why is special.com also redirecting to website.com and not to website.com/some-special-page ?
The overall redirect for all the other domains is working fine, however I can't figure out how to have this special case work (and not be overwritten by the second rule. Thanks in advance!

301 redirect from non-www to www version

I'm trying to redirect my non-www site to its www version, to this end I put the below code in the .htaccess in root folder of my site:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
But problem is that after all this configuration I get this error when trying to access it:
The page isn’t redirecting properly
Both www and non-www address of my site doesn't work.
Is anything wrong with that code?
This situation arises on a server that hosts multiple domains. Mostly such servers are shared hosts or reseller type of hosting arrangements. Use the following code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?example\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
I've found the solution here

Redirect subdomain to different locations based on url

I have a website on a domain that I want to move to another domain. Here is my problem. The root URL has to redirect to a different location on the new domain. Example:
http://subdomain.olddomain.com/ must redirect to http://www.newdomain.com/location
http://subdomain.olddomain.com/code/slug must redirect to http://www.newdomain.com/slug
The htaccess code that I have doesn't work ver
RewriteRule ^/?(.*) http://www.newdomain.com/$1 [R=302,L]
This code works for the second example, but nog on the first. Can anyone help me setup the correct htaccess code?
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.olddomain\.com$ [NC]
RewriteRule ^code/(.*)$ http://www.newdomain.com/$1 [L,R]
RewriteCond %{HTTP_HOST} ^subdomain\.olddomain\.com$ [NC]
RewriteRule ^$ http://www.newdomain.com/location [L,R]

Resources