subdomain redirect to https - .htaccess

I have a redirection rule:
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
this redirects all my domain requests to https.
For example:
I call www.example.com and I get https://www.example.com
Now my problem:
I would like to call test.example.com and I would like to get https://www.example.com/test.
How can I realize it?

I would like to call test.example.com and I would like to get https://www.example.com/test
Put the following before your existing HTTP to HTTPS redirect:
RewriteCond %{HTTP_HOST} ^test\.example\.com [NC]
RewriteRule ^$ https://www.example.com/test [R,L]
Note that if /test is a physical subdirectory then you should append a trailing slash, otherwise you'll get a second redirect.

Related

How to correctly redirect all http requests to https

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.

Domain redirect only for primary domain not add on domains

I want to redirect all http request to https. The problem I am experiencing is that it redirect all add on domains too. I want to redirect only primary domain. Here is my htaccess code -
This will enable the Rewrite capabilities:
RewriteEngine On
This checks to make sure the connection is not already HTTPS:
RewriteCond %{HTTPS} !=on
This rule will redirect users from their original location, to the same location but using HTTPS. The leading slash is made optional so that this will work either in httpd.conf
or .htaccess context:
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
What can I do to make sure it only redirects on the primary domain?
If you want to redirect a specific domain from http to https you can use the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://% {SERVER_NAME}/$1 [R,L]
This will redirect http://example.com/ to https://example.com/ .

Force HTTPS too many redirect with .htaccess, how to resolve?

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}).

How to redirect https file to http file

I want to redirect all page from https to http using this code.
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ http://aplauz.media.pl/$1 [R=301,L]
It works!
I want to redirect specific file https to http in the same time.
Redirect 301 /aktualnosci/155-rusza-kampania-promujaca-w-polsce-rosji-i-chinach/ http://aplauz.media.pl/blog/rusza-kampania-promujaca-w-polsce-rosji-chinach/
It works as well!
BUT NEVER BOTH because browser inform me about double redirection.
Anyone can help me?
You can use:
RewriteEngine On
RewriteRule ^aktualnosci/155-rusza-kampania-promujaca-w-polsce-rosji-i-chinach/ http://aplauz.media.pl/blog/rusza-kampania-promujaca-w-polsce-rosji-chinach/ [L,NC,R=302]
RewriteCond %{HTTPS} on
RewriteRule ^ http://aplauz.media.pl%{REQUEST_URI} [R=302,NE,L]
i.e. keep specific redirect rule before generic catch-all rule.

.htaccess Redirect to HTTPS except subdomain

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

Resources