Redirect to https always with www optimization using htaccess - apache

I need to redirect everything to my domain to use https://www and below is the .htaccess I am currently using:
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
It is working but it makes 2 redirects to the browser one if missing the www and the second if missing the secure which is slow of course and may be bad.
What I want or my question is can this be reduced to single redirect rule to make it add both the www and the https in one rule.
This online test tool shows 2 redirects https://varvy.com/tools/ :
Final status code: 200
2 Redirect(s)
http://domain.com
301 redirect
https://domain.com/
https://domain.com/
301 redirect
https://www.domain.com/
Any good optimizations to this code.

You can use:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
2 rules but never more than one redirection

You can use just 1 single rule
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
non-www to www redirect is not needed here because you want to redirect both versions to https://www .
Clear your browser's cache before testing this.

Related

Using APACHE REWRITE To Force One Specific Page To Be HTTP (.htaccess)

I have tried using some rewrite to change my /welcome.php page to http. it is located in the root directory, but what I have done has seemingly forced all pages on my site to http instead of just the one. Here is what I have tried:
RewriteEngine On
#Force remove WWW
RewriteCond %{HTTP_HOST} ^www.w5unt.ga
RewriteRule (.*) http://w5unt.ga/$1 [R=301,L]
#Redirect HTTPS to HTTP
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
As you can see I am also force removing WWW from all urls at the same time I am trying to force my one page (welcome.php) to be http. I believe the error is in this bit of code
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
However I am not sure how to syntactically correct my issue, any advice?
Try with below I've edited the part of your rule and made a check to exclude welcome.php.
RewriteEngine On
#Force remove WWW
RewriteCond %{HTTP_HOST} ^www.w5unt.ga
RewriteCond %{REQUEST_URI} !^/welcome.php
RewriteRule (.*) https://w5unt.ga/$1 [R=301,L]

Multiple URL redirections happening from non https www to https www

Have a small wordpress blog and was following this guide. One of the step was to change htaccess file for redirects for non-www and www to https www. According to https://varvy.com/tools/redirects/ there should only be one redirect on each type.
But for my no www no http to https www there is 2 redirect happening.
http://myportal.com
301 redirect
https://myportal.com/
https://myportal.com/
301 redirect
https://www.myportal.com/
Many of the tools are taking this as a negative and saying too many redirects. How can I make the 2 step into 1 step? So the result comes to
http://myportal.com
301 redirect
https://www.myportal.com/
Currently have the following code.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You can use the following rule to force https and www in a single URL redirection.
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*)$ https://www.%1%{REQUEST_URI} [L,R=301]
Note : Remove the non-www to to www redirect rule If you already have that rule in your htaccess otherwise that might conflict with this one.
Make sure to clear your browser cache before testing this new rule.

.htaccess - multiple page by page redirects + www & https rules

Been struggling and playing with this and can't get it working in Apache.
I'm not well versed in the syntax and was rather trying to build something based on existing questions on here.
Trying to force everything to HTTPS and www, and also add over 200 individual page redirects.
RewriteEngine on
# Redirect to domain with www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Same for HTTPS:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# 301 Redirect URLs.
Redirect 301 /PAGE1 /PAGE2
Thanks a lot in advance!
in thw www part remove the https condition and redirect with https and www anyway
in the https part remove the www condition
after those 2 ... continue with all your paged redirecting
RewriteEngine on
# Redirect to domain with HTTPS.
RewriteCond %{HTTPS} off
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Same for www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# 301 Redirect URLs.
Redirect 301 /PAGE1 /PAGE2

Redirect certain pages from http to https; redirect rest of site to www

My requirements are as follows:
Certain pages of my site need to use https
The rest can use http
I also need to redirect everything to use a www. prefix (e.g., if someone visits domain.com it redirects to www.domain.com)
I've tried a number of solutions listed here on Stack Overflow, but none seems to work.
Here's the relevant portion of my htaccess file:
#force https for certain pages
RewriteCond %{HTTPS} !=on
RewriteRule ^(login2\-test\.php.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
#redirect all pages to www
RewriteCond %{HTTPS} !=off
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Redirecting all pages to www works fine. However, the redirect of login2-test?Switch=ftr (which is in the reg/ subdirectory) to https results in no match, so the page is displayed using http.
I've rewritten that line as follows:
RewriteRule ^(reg\/)(login2\-test\.php.*)$ https:www.domain.com/$1$2 [L,R]
This at least matched / redirected to https, however the browser could not resolve it.
I've swapped the order of the rules (e.g., www redirect first); that didn't help.
I would appreciate any help -- I've been struggling with this for a while now.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

how to configure apache mod_rewrite to redirect to non-www version with two exceptions

I'd like to redirect incoming requests to our https:// non-www version of the site using a 301 redirect. It is required to exclude two subdomains specifically (webmin.domain.com and newsletter.domain.com)
This is what I have so far
RewriteEngine on
RewriteCond %{HTTPS_HOST} !^(newsletter|webmin)\.domain\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com$1 [L,R=301]
The problem with the above is, that it creates an infinite loop because it seems that the RewriteCond matches the non-www version of the URL.
How can I extend this rewrite rule properly?
Try the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!newsletter|webmin)[^.]+\.domain\.com$ [NC]
RewriteRule ^.*$ https://domain.com/$0 [R=301,L]