Redirecting two pages from HTTPS to HTTP - .htaccess

I have two pages I need to redirect from HTTPS to HTTP. I have working code for a single page but I am struggling to figure out how to get it working for two pages.
It redirects request to /modules/gateways/callback/pne.php
I need it to also do requests to /modules/gateways/callback/two.php
Simply duplicating the same code below it causes a redirect loop it seems.
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/modules/gateways/callback/one\.php$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L,QSA]
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(modules/gateways/callback/one\.php)$ http://%{HTTP_HOST}/$1 [R=301,L,QSA]

Seems it was a simple as removing the first rule, as I actually don't need all other requests auto redirected.
This is the final code.
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(modules/gateways/callback/one\.php)$ http://%{HTTP_HOST}/$1 [R=301,L,QSA]
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(modules/gateways/callback/two\.php)$ http://%{HTTP_HOST}/$1 [R=301,L,QSA]

Related

Redirect BOTH http and www to https://example.com

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.

http to https redirect not working via .htaccess

I have picked up the following .htaccess snippet from another thread but on the site it is being tested on, I get a TOO_MANY_REDIRECTS Error.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.co.uk/$1 [L,R=301]
If it helps the site is hosted on 123-reg.co.uk.
I have tried multiple variations to redirect non-www to www and then http to https but the same error each time.
This one is working fine, ignore the later one.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
[Ignore this ] How to force SSL with .htaccess (Simple based on port 80 means http) - redirect all request to 443 automatically
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://yourdomain.com/$1 [R,L]
Ref; https://www.siteground.com/kb/how-to-force-ssl-with-htaccess/

htaccess to redirect whole website to https doesn't work

I am using the below script to redirect traffic to https. It work fine if I just type domain.com in the address bar but if I type www.domain.com it doesn't get redirected. How do I make it redirect both with and without www?
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
Try this:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
In case you wish to force HTTPS for a particular folder you can use:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} somefolder
RewriteRule ^(.*)$ https://www.yourdomain.com/somefolder/$1 [R,L]
Thanks

Mod Rewrite - multiple condition

I have a certain sub-domain which requires an SSL connection for all pages on that subdomain.
This is my current .htaccess script:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
I have a single page on the subdomain which must not have SSL access (causes issues with accessing files on another server without a certificate).
My mod_rewrite knowledge is very limited. I've done a search but can't find what I need.
My question is, the page which must not have https is called 'tutorial.php'. Is there a way to redirect all pages except tutorial.php to https?
I guess in pseudo-code, the RewriteCond would be similar to:
RewriteCond %{SERVER_PORT} 80 AND {WEB_PAGE}!='tutorial.php'
Thanks
Try this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !/tutorial\.php
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
RewriteCond %{HTTPS} on
RewriteRule ^tutorial\.php$ http://%{HTTP_HOST}/tutorial.php [R,L]

htaccess, Always https://www

Currently using the following to make sure my website always is using www. How would I modify this to be www and https?
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Tried this, but ended up with www.www.website if the user had no https but did already have a www
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Not sure how to combine the two
Make us of {SERVER_PORT} to see if the request is made via regular connection (80), then redirect to the https URL.
This should do it:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Here are some nice example, using different methods.

Resources