htaccess redicect to www exclude subdomain - .htaccess

i have the following rule to redirect non www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
but now i want a subdomain assets.company.com το be excluded but nothing seems to work for me
i have try
RewriteCond %{HTTP_HOST} !^assets\. [NC] [AND]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
and its not working
Pleas help

You can use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|assets)\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Make sure to keep this as first rule.
Make sure to test this in a new browser to avoid caching issues.

Related

Forward non-www to www except for specific subdomain

I am routing all non-www requests to the www-domain by using this RewriteCond and RewriteRule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.mydomain.tld/$1 [R=301,L]
But there is one certain subdomain that I want to exclude, e.g. blog.mydomain.tld. How do I have to modify my condition and rule in order to achieve this?
You can just add that subdomain in regex of your RewriteCond:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|blog)\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,NE]
Did you try adding
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^blog\.
RewriteRule ^(.*)$ http://www.mydomain.tld/$1 [R=301,L]

Htaccess rewrite rule and condition

We are now using OpenCart and domain aliasses in DirectAdmin.
We now have some rules in our htaccess for redirecting non www to www and non https to https.
But this rule is for all domains. We only want to use it for example-1.com
How can we do this? Htaccess we have tried all failled to do this.
This is the current htaccess we have
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https\:\/\/www.example-1\.com\/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https\:\/\/www.example-1\.com\/$1 [L,R=301]
We only want this for example-1 and example 2 also. But now it always redirects to example-1
I understand you only try to redirect example-1 and example-2 and not other domains.
You can use that:
RewriteCond %{HTTP_HOST} (?:^|\.)(example-1\.com|example-2\.com)$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R=301]
Try:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [L,R=301]

Pointing multiple domains to specific pages via htaccess

Let's say may main domain is www.mysite.com and that I'm using the following rewrite in my hataccess
RewriteCond %{HTTP_HOST} !^www.mysite.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
All good so far! However, I also need other domains to redirect to specific pages on the site. For example, if somebody types www.mysite.nl they will be redirected to www.mysite.com/neatherlands. The issue is when I pop the below into my htaccess file I get a server error. I think this is because of the final rewrite. It's a pretty mad set-up
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.nl$ [NC]
Rewriterule ^(.*)$ http://www.mysite.com/netherlands/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.be$ [NC]
Rewriterule ^(.*)$ http://www.mysite.com/belgium/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.co.\uk$ [NC]
Rewriterule ^(.*)$ http://www.mysite.com/$1 [R=301,L]
Any suggestions about what to do?

Redirecting specific Domains to HTTPS

My domain www.abc.com is now redirecting to https://www.abc.co.uk
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
These code works exactly what I want. After that I created a subdomain. www.x.abc.co.uk
Now I want to redirect that to http://x.abc.co.uk (without https)
I used this
RewriteCond %{HTTP_HOST} ^x\.abc\.co\.uk [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But browser says it has redirect loop.
How can do that?
If these are two different htaccess files then give this a try.
RewriteEngine On
#redirect www.x.abc.co.uk without www to http
RewriteCond %{HTTP_HOST} ^www\.x\.abc\.co\.uk [NC]
RewriteRule ^(.*)$ http://x.abc.co.uk%{REQUEST_URI} [L,R=301]
I modified your original rule so you don't have two rewrite rules and still direct to www.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*)$ https://www.abc.co.uk%{REQUEST_URI} [L,R=301]

Htaccess redirect to www except a specific file

I want to redirect non-www to www, and exclude a specific file (eg. testit.php). Cant find a working solution...
RewriteCond %{REQUEST_URI} !^testit.php
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This redirects all files, including testit.php (which i want to exclude).
Thanks for help!
RewriteCond %{REQUEST_URI} !^/testit.php
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Resources