htaccess non-www and non-subdomain to www - .htaccess

I am using htaccess to redirect non-www to www like this:
user typea: mydomain.com
.htaccess redirecta to www.mydomain.com
My code is:
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
But I don't want that redirection when a user enter a subdomain: help.mydomain.com
(help.mydomain.com is directly sent in mydomain.com root)
Do you have any idea what changes I should do to get this possible?

Yes it's possible. Here's how:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

Related

How can I redirect all pages from www to non-www and also http to https with htaccess?

I have been able to find solutions to each of these redirects separately but I can't find something that does both correctly.
I need the website to redirect from:
http://somesite.com.au to https://somesite.com.au
http://www.somesite.com.au to https://somesite.com.au
https://www.somesite.com.au to https://somesite.com.au
The main combination that I'm having trouble with is:
https://www.somesite.com.au to https://somesite.com.au
Currently I am using the following in my htaccess file:
# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Ensure we are using HTTPS version of the site.
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect www to non-ww
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
What should I be using in my htaccess to make this work?
You're causing a loop because your www rule redirects to http://. Just change it to redirect to https:// instead:
# Redirect www to non-ww
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

htaccess: non-www to www but only when no subdomain is set

I want this:
domain.tld to www.domain.tld
And other subdomains: sub.domain.tld without redirect to www.sub.domain.tld
You can put this code in your htaccess (in root folder)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.tld$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

htaccess redirect no-www to www (multidomains)

I need to redirect non www URL to WWW URL for multidomains
here is what I need exactly
http://example.com should goto www.example.com
http://example.de should goto www.example.de
All the above geographilay domains should do to their corresponding domains with www (.com .de and .uk are in same server)
Try this :
RewriteEngine On
RewriteCond %{HTTP_POST} !^www\. [NC]
RewriteCond %{HTTP_POST} ^(([^\.]+)\.([a-z\.]+))$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
All credit go to MrWhite his solution here:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Redirect non-www subdomain

Using these directives, I can redirect any non-www subdomain to the www subdomain:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
What I need to achieve is to redirect the www subdomain only.
For example I want to redirect only blog.myname.com to www.blog.myname.com. The rule in the config above will redirect blog.myname.com to www but it also redirect myname.com to www.myname.com, which is not what I am looking for. I also need to redirect all queried subdomain not just blog.myname.com
How can I do this?
You can use this code to achieve that:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www)[^\.]*)\.myname\.com$ [NC]
RewriteRule ^ http://www.%1.myname.com%{REQUEST_URI} [L,R=301]
Above code withh redirect all subdomains but will not effect myname.com or www.myname.com.
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.myname\.com$ [NC]
RewriteRule ^ http://www.blog.myname.com{REQUEST_URI} [L,R=301]

Force www to directory using .htaccess

I have this application directory
http://www.example.com/application
When user browse without www, (e.g. example.com/application), i want to force and redirect them to http://www.example.com/application
How can i achieve using .htaccess.
Thank you.
To redirect non www to www:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
To redirect www to non www, the code is similar:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^my-domain\.com$ [NC]
RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]

Resources