Add automatically HTTPS if it's not a subdomain. - .htaccess

My website (https://www.went.digital) have a wildcard subdomain (but sadly not a wildcard SSL), which redirects to a subfolder. That's mean that if you enter to demo.went.digital, you'll see what's in went.digital/subdomain. It doesn't really matter how does it work, but I'm saying it because the .htaccess file affecting the subdomains too.
I add into the .htaccess file code that automatically adds www if it's not a subdomain, and I add to it https:// but it doesn't add the https:// if you entered using www.
Here is my current code in the .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]

You'll have to add a separate rule for that:
RewriteCond %{HTTP_HOST} ^www\.went\.digital$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.went.digital/$1 [L,R=301]

Related

Exclude sub domain rewrite non www to www in htacces

I'm having a Magento multistore setup and i'm sending all non www requests to www in the .htacces with the rewrite:
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I'm using a sub domain for testing new websites and for this sub domain I don't need this rewrite. Therefore I use:
RewriteCond %{HTTP_HOST} ^sub\.domain\.com [NC]
RewriteRule ^(.*) - [L]
Only problem now is that when I go to http:// sub.domain.com it works, but when I go to a category or product for example http:// sub.domain.com/cat1 or http:// sub.domain.com/product-red.html it doesn't work any more and I get a "Not Found" message.
What do I need to add to the code so it excludes the whole sub domain, including everything after the / ?
Don't use a separate rule to ignore all requests for subdomain like that otherwise Magento's routing rule will also be skipped. Instead tweak your www rule like this to ignore subdomain:
RewriteCond %{HTTP_HOST} !^(www|sub)\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}/$1 [R=301,L,NE]

301 redirect to new domain with same url structure

I have a Magento website that is moving to a new domain. Im looking to 301 redirect all pages from the old domain to the new domain keeping same url structure.
I've updated the .htaccess file on my old domain with:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^new-domain.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [R=301,L]
The issue: The above seems to redirect every instance of: old-domain.com/subdir/whatever only just to the main domain: new-domain.com.
I'd be looking to redirect old-domain.com/subdir/whatever to: new-domain.com/subdir/whatever.
Any idea on what could be wrong?
Adjust your rule to use REQUEST_URI variable which is not dependent on the directory of .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://new-domain.com%{REQUEST_URI} [R=301,L,NE]
Better to clear your browser cache before testing this rule.
PS: You need to match hostname condition to old-host not the new-host.
This should redirect and retain the path after the main domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^old-domain\.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [R=301,L]

htaccess non www to www and remove subdomain www

So I've got the following rewrite code in my htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
//
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.([^\.]*)\.domain\.com$ [NC]
RewriteRule (.*) http://%1.domain.com$1 [R=301,L]
Works perfect for redirecting non-www to www for my domains.
I've got a subdomain, lets call it 'sub.domain.com' which works find. If I goto www.sub.domain.com, it redirecting to 'sub.domain.com/sub/'
Anyone having a idea why?
None of the rules you have in your question routes requests to a subdomain's folder. The /sub/ should never be there if it wasn't originally in the request.
That said, all of your redirect rules need to come before any routing rules. Routing rules being stuff that internally routes requests to other URI's, for example:
RewriteCond %{HTTP_HOST} ([^\.]*)\.domain\.com$ [NC]
RewriteRule (.*) /%1/$1 [L]
That rule internall routes to a folder named the same thing as the subdomain. If this rule were to be before the redirect, both rules get applied and the redirected URI becomes /sub/. You need your routing rules placed after your redirect rules, e.g. all rules that have a http:// or an R flag.

Redirect to domain always with WWW attached causes error with subdomains

I have the code below in my website's .htaccess file:
#redirect non www to http://wwww
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This works fine but when I decide to access the forum on my site as a subdomain, the .htaccess is screwing everything.
So normally the url to forum is:
http://www.eetutorials.com/forum
but since I linked the forum folder as a subdomain, it does not work anymore and redirects to:
http://www.forum.eetutorials.com/forum/
which obviously is wrong! any idea how can I fix that by adding some conditions into .htaccess file?
You could change a condition:
RewriteCond %{HTTP_HOST} !^www\.
to this one:
RewriteCond %{HTTP_HOST} ^eetutorials.com$

Optimize htaccess Wildcard Subdomain Code

I have Wildcard Subdomains working perfectly on my server, but would like to optimize the .htaccess code to make it more portable. I'm not sure how to do it.
I would like to replace the specific domain name and extension (DOMAIN and .com in the code below) with a regex match so that when using the wildcard code for other domains it is easy to port. So it should be a drop in replacement for domain.com and domain2.net. Here's the code I use:
# Wildcard Subdomain
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+)\.DOMAIN\.com [NC]
RewriteCond %2 !^www$ [NC]
RewriteRule ^(.*)$ http://DOMAIN.com/$1?a=%2 [R=301,QSA,L]
It's a little different than the usual version you see, because I've added the exception for the www. + subdomain condition. It many cases people enter www.sub.domain.com which breaks on the server. So I check for the www. and remove it before redirecting.
Thanks for your help!
The way you currently have it:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteCond %2 !^www$ [NC]
RewriteRule ^(.*)$ http://%3.%4/$1?a=%2 [R=301,QSA,L]
If you just want to strip the www from the domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteRule ^(.*)$ http://%2.%3.%4/$1 [R=301,QSA,L]

Resources