Redirect non-www subdomain - .htaccess

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]

Related

redirect all subdomains with htaccess

I have some unused subdomains and I would like to redirect them to my homepage.
I have found this .htaccess code
# redirect all subdomains
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com/ [L,R]
It redirects when you call the exact subdomain, for example: test.example.com
But it does not work if you call anything else, for example: test.example.com/meh
It's possibile to redirect any URL in these subdmoains to the homepage?
You may use this rule:
# redirect all subdomains
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301,NE]
%{REQUEST_URI}at the end of target will add previous URI to new target.

.htaccess redirect www to non-www for subdomain that is before the www

I need to make a request to https://subdomain.www.domain.com rewrite the URL to https://subdomain.domain.com
Which part of the below rewrite do I change to achieve this? I'm guessing I have to put the subdomain in front of the www, but what do I put there, also how do I capture all subdomains - what's the general rule for all subdomains?
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
You may use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.(domain\.com)$ [NC]
RewriteRule ^ https://%1.%2%{REQUEST_URI} [R=301,L,NE]

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 and non-subdomain to www

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]

Redirect .com to .org in .htaccess

I have domain.com and domain.org as aliases pointing to the same vhost. How can I use .htaccess to redirect all domain.com requests to domain.org?
You could use mod_rewrite to do this.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^example\.org$
RewriteRule ^ http://example.org%{REQUEST_URI} [L,R=301]
This rule redirects every request that’s not addressed to example.org to the very same.
Redirects all www/non-www of domain.com to domain.org:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.org/$1 [R=301,L]

Resources