I use this code for redirecting http to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTPS_HOST} !^www.tronsoeskolen.dk$ [NC]
RewriteRule ^(.*)$ https://www.tronsoeskolen.dk/$1 [L,R=301]
But it send every page with http to https://frontpage no matter it says after the domain.
Rewrite conditions are combined by AND, as long as you don't define anything else.
So there is no condition for handling your request without www.
Your rules do the following:
If it is not an https request AND the request comes not from host https://www.yourdomain.xy, then rewrite to https://www.yourdomain.xy.
Combine your conditions by [OR] (example):
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
This combines the conditions to: if it is not an https request OR a request without www.
Related
The goal is to permanently redirect BOTH HTTP and www to https://example.com on a shared hosting service with an active SSL.
Several attempts; some involving a rule like:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
and other attempts involving, for example, rules like the StackOverflow post by Rahil Wazir involving:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{SERVER_PORT} !^443
RewriteRule ^(.*)$ http://example.com/$1 [L, R=301]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{SERVER_PORT} ^443
RewriteRule ^(.*)$ https://example.com/$1 [L, R=301]
Thus far, no attempt has completely met the goal. Can you help?
Note that https://www.example.com should also permanently redirect to https://example.com.
Providing you are not implementing HSTS (in which case you should implement two separate redirects, the first being HTTP to HTTPS on the same host) then you can do something like the following at the top of your .htaccess file:
RewriteEngine On
# Redirect HTTP to HTTPS (and www to non-www)
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://example.com/$1 [R=302,L]
The HTTPS server variable is an alternative (and arguably more readable) way of checking to see whether HTTPS is enabled or not, instead of explicitly checking the SERVER_PORT.
Note this is a 302 (temporary) redirect. Only change it to a 301 (permanent) redirect when you have tested that it is working OK.
You will need to clear your browser cache before testing.
Problems with the directives you posted:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
This only redirects HTTP to HTTPS. It won't canonicalise a request for https://www.example.com/. This is also a temporary (302) redirect.
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{SERVER_PORT} !^443
RewriteRule ^(.*)$ http://example.com/$1 [L, R=301]
Apart from being syntactically invalid (you have a space inbetween the RewriteRule flags - which will result in a 500 Internal Server Error), this only redirects www and HTTP. But it redirects to HTTP, not HTTPS! Consequently, it won't canonicalise/redirect http://example.com/ or https://www.example.com.
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{SERVER_PORT} ^443
RewriteRule ^(.*)$ https://example.com/$1 [L, R=301]
Again, the RewriteRule directive is syntactically invalid for the reason mentioned above. This only redirects www and HTTPS. Consequently, it won't canonicalise http://example.com/ or http://www.example.com.
Hi I'm trying to do the following.
When the domain is accessed through http it should redirect to https.
When domain contains www it should redirect to https
When domain contains language abbreviation it should remove it.
Possible urls:
1: http://www.example.com/en/how-did-it-all-start
2: http://example.com/en/how-did-it-all-start
3: http://www.example.com/how-did-it-all-start
These domains should redirect with one 301 redirect to
https://example.com/how-did-it-all-start
I have tried numerous things but nothing seams to work when I'm trying to combine it with the www. It now only works for http urls, when I add the www to it, it breaks.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{REQUEST_URI} ^en/(.*)$ [NC]
RewriteRule ^ https://%1%/%2% [R=301,L]
I don't know how to properly combine those to conditions without breaking it.
Tried to do in different ways but until now no success.
Any help would be appreciated.
Thanks in advance.
You need to have [OR] clauses in your conditional with optional matches to handle all 3 cases:
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(?:en/)?(.*)$ https://%1/$1 [R=301,L,NE]
I would like to forward all requests to a domain via .htaccess.
Everything to: https://domain.tdl
No-www
https
I have tried many spelling, but it has not worked for completely.
I think the following single set of rule-set should work:
RewriteEngine On
RewriteCond %{HTTPS} off [NC,OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://domain.tld%{REQUEST_URI} [R=301,L]
I have subdomain: subdomain.example.com
I would like to redirect http and https to another domain
http://subdomain.example.com --> https://subdomain.exampleNew.com
https://subdomain.example.com --> https://subdomain.exampleNew.com
Does anyone know how to do this?
Currently I have only:
RewriteCond %{HTTP_HOST} ^subdomain.example.com$ [NC]
RewriteRule ^(.*)$ https://subdomain.exampleNew.com/$1 [R=301,L]
If you want to retain the http/https status, the status needs to be captured first and then passed to the rule as %1. Use the following instead:
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond %{HTTP_HOST} ^subdomain.example.com$ [NC]
RewriteRule ^(.*)$ http%1://subdomain.exampleNew.com/$1 [R=301,L]
I'd like to force all http traffic to https, and also always force www.
This is what I have so far:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
This seems to correctly work when the url does not contain www. So http://mydomain.com correctly redirects to https://www.mydomain.com.
However, it is not correctly redirecting to https when the www part is present. So, www.mydomain.com is not redirecting to https://www.mydomain.com
Edit
I have got this working with two rewrite blocks:
# Force ssl
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# redirect non-www to www
RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
No idea if this is acceptable or not, but it works
Use your domain name and not HTTP_HOST in the rewriterule so that way it won't matter if www is there or not and you can use 1 rule. You can have two rewrite rules but just use OR. I think you were looking for is this.
# always redirect to www and/or https
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC,OR]
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*) https://www.mydomain.com/$1 [R=301,L]