I would like to redirect all none-https requests to https excepts requests to sub-domains. For example
http://example.com/ => https://example.com/
http://example.com/page => https://example.com/page
But
http://m.example.com/ REMAINS http://m.example.com/
This is what I have in my .htaccess, which redirects all requests (including sub-domians):
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
I know that I have to add a condition before the RewriteRule but I am quite not sure about the syntax.
Add another RewriteCond before your RewriteRule:
RewriteCond %{HTTP_HOST} !=m.example.com
To Exclude any Subdomain from https rewrite add
RewriteCond %{HTTP_HOST} !=/(.*?)/.example.com
Related
I have a functional URL shortening site. Recently I added SSL to the website and configured the .htaccess for proper redirecting. The issue is that all of the http:// requests, redirect back to the homepage https://websi.te
Currently, I've tried setting up redirecting as follows:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
An example of what's going wrong:
User types http://websi.te/keyword or websi.te/keyword and they are redirected to https://websi.te instead of https://websi.te/keyword
Change your rewrite to this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
$1 is first group match ^(.*)$, it's your request.
I am searching for the perfect 301 redirect HTTP to HTTPS - non-www to www. But I can't find a working solution.
Here is what I want to do
http://domain.tld/ → https://www.domain.tld/
http://www.domain.tld/ → https://www.domain.tld/
https://domain.tld/ → https://www.domain.tld/
In this post: Best Practice: 301 Redirect HTTP to HTTPS (Standard Domain)
It concludes:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]
but this is for www. to non-www.
Please help.
Thanks,
Not sure if you need this for multiple domains, or just one, but here is an example where multiple domains are supported. Since you want www and https on all domains, I've just hardcoded it. This should also skip rewriting if there is a sub-domain in the url. You might need to adjust it to support TLDs with dots (i.e. .co.uk, .co.au etc).
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
I know this has been asked and answered several times and I have gone through a lot of them but still have not solved my issue.
I have the following code in my .htaccess file.
RewriteEngine On
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
I want to redirect all of the following:
http://www.example.com
http://example.com
https://example.com
to redirect to https://www.example.com.
Currently it comes up to an error page stating too many redirects, I have tried clearing cookies to no avail.
What am I doing wrong and how can I make it work?
Try the following:
RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)
RewriteRule ^ https://www.%1%{REQUEST_URI} [R,L]
This canonicalises both the HTTPS and www subdomain in a single rewrite. Basically, this says... For all requests that are not HTTPS OR where the hostname does not start www then redirect to https://www.example.com/<whatever>.
The %1 backreference holds the hostname, less the optional www subdomain, from the preceding CondPattern that is always matched.
You need to use the server variable HTTPS (ie. %{HTTPS}), not the environment variable HTTPS (ie. %{ENV:HTTPS}).
Currently I have the following in my .htaccess file for a forum site.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^/?$ http://example.com/forums/ [L,R=301]
It works perfect for redirecting everything like (http)://example.com, www.example.com, (http)://www.example.com etc. to example.com/forums which is what I wanted.
I now want to have everything redirect to (https)://example.com/forums but am running into problems with existing links such as example.com/forums/stuff.
I can only get example.com to redirect to https://example.com/forums, any direct links such as example.com/forums/stuff etc. will not redirect to the https version, any ideas?
You need 2 redirect rules. One rule to redirect landing page to forums page and another rule to redirect http -> https:
RewriteEngine On
# redirect landing page to /forums/
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com [NC]
RewriteRule ^/?$ https://%{HTTP_HOST}/forums/ [L,R=301]
# redirect http -> https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
I was trying to redirect my webpage to normal http but one specific url should be redirect to https.
I tried this:
RewriteCond %{HTTPS} off
RewriteRule ^site-name\.html$ https://domain.com/site-name.html [L,R=301]
But this way, the normal website is reachable through http and https.
Maybe you guys can help me with that.
You can have another to redirect https->http for all other pages:
RewriteCond %{HTTPS} on
RewriteRule !^site-name\.html$ http://domain.com%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^site-name\.html$ https://domain.com%{REQUEST_URI} [NE,L,R=301]