Redirect subdomain to https with htaccess - .htaccess

I want to redirect all URLs only from one subdomain to https.
My URL http://nip.domain.com/sitexy.html should be https://nip.domain.com/sitexy.html
With this code I will be redirected to the home page with https and the path will be removed:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(nip)\. [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R]
Can anybody give me a hint, how to keep the path behind the domain?
Thanks!

Related

Redirect [http and/or non-www] to [https://www.] in ONE redirect

I have setup redirects in the htaccess file. It redirects all non-https and non-www requests to https-www versions.
Following are the rules I have used in the .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The redirect works fine. My only problem here is that it requires 2 redirects. Let's say my website is example.com.
When I open example.com, it first redirects to https://example.com and then it gets redirected to https://www.example.com
So, I get the response code after two redirects. 301-301-200
Now my question is: Is there any way to have it done in a single redirect?

Non-www to www and http to https in htaccess (SITE-WIDE redirects!)

I'm trying to redirect non-www to www and http to https on a SITE-WIDE level. That means for every page, not just the root domain. The below works but not when I type other pages like example.com/blog
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
The above works, except it isn't site-wide. Its good for the root domain, but as soon as I type example.com/blog then this all doesn't work. Please provide the site-wide redirects for htaccess non-www to www and http to https. All the similar questions don't focus on site-wide redirect so please focus on this issue.
Thanks in advance.

Redirect HTTP to HTTPS Htaccess

I need a script to .htaccess to 301 redirect http to https, but a page needs to be accessed by HTTP. Example:
example.com/* redirects to https://example.com/*
example.com/page1 not redirect to https://example.com/page1
Just use this:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
So it will just set your SSL to https for all pages, even if someone access it via http.
Use this if you want to force https for all except the directory /page1
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !page1 [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Htaccess redirect https to http for all pages except homepage, redirect https for homepage to another url

I am trying to redirect all https request to http excluding the homepage but i also want the https request for homepage to redirect to another url.
here is what i have in my .htaccess right now
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
the rule above redirects everything except homepage but i want the homepage redirected to another url e.g
https://www.example.com/
redirects to
http://www.example.com/non-secured-page
i need a full .htaccess rule that can do this.
You can use:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.+) http://www.example.com/$1 [R=301,L,NE]
RewriteCond %{HTTPS} on
RewriteRule ^/?$ http://www.example.com/non-secured-page [R=301,L]

123-reg subdomain redirect to secure URL

I manage the DNS of a URL through 123-reg. I'm looking to create a subdomain and have it redirect to a specific folder of a secure URL for example https://sub.url.com/folder. How can I do this?
You can do this with some htaccess on the root folder of your domain.
ie:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(sub.)?url.com$
RewriteRule ^(/)?$ FOLDER [L]
Re-reading your question..
if users come on a non secure URL, you can always redirect them first to HTTPS like so
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
then redirect to the subfolder.

Resources