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

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]

Related

Rewrite subdirectory to https

I have been trying to rewrite a subdirectory from HTTP to HTTPS to no avail. I have looked at other posts and tried to implement the solutions:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} subdirectory
RewriteRule ^(.*)$ https://www.exmaple.com/subdirectory/$1 [R,L]
and also
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(subdirectory/.*)$ https://www.example.com/$1 [R=301,L]
and placing them either on the root directory .htaccess file or the Plesk's Apache and Ngnix settings. But whenever I typed
http://www.example.com/subdirectory/some.html
I will always get
https://www.example.com/subdirectory//subdirectory/some.html
which is of course 404 not found. Any assistance is appreciated.
To force HTTPs for just a single directory then what you can use is:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{REQUEST_URI} ^\/(subdirectory)
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} !^\/(subdirectory)
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
What is the above doing? The first condition checks if HTTPs is not on, if not, then it will check for the directory /subdirectory/ if found it will force this directory and only this directory to HTTPs.
The second set of rules is basically the opposite, forcing everything to HTTP except for the directory /subdirectory/.
Make sure you clear your cache before testing this.

.htaccess redirect to https if not http and add forward slash if it doesn't exist in one redirect

I'm having some trouble getting multiple redirect rules working in an .htaccess file without it causing a redirect chain:
First I want to force HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
Then I want to ensure a trailing slash is applied
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301]
However, I want to do these both as ONE redirect. So if a request comes in with:
http://example.com/page
in ONE 301 redirect it should give
https://example.com/page/
I'm using httpstatus.io to test the redirect chains. Many thanks
Try with below,
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}/ [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]

http to https causing redirect loop

I wish to forward all visitors to my site to use HTTPS instead of HTTP. Whatever they request, I want to server over HTTPS.
I've tried adding this to my .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
But it just causes a redirect loop. I'm guessing RewriteCond isn't functioning as expected?
This should work :
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NE,NC,R,L]

Why won't this htaccess exclude condition work?

I'm forcing https on all pages on our site, but because of complications with some old plugins we use (needing to connect over http), I need to set up an exception to the https forcing for one directory.
I can't quite work it out though. The rule I think should work is giving me a 403 error.
Can someone have a look?
I've got a bunch of stuff for redirecting non-www to www:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^subdomain1.site.com$
RewriteCond %{HTTP_HOST} !^subdomain2.site.com$
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301]
Then there's the https forcing:
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.site.com/$1 [R,L]
But if I add another condition to exclude the directory from this rule:
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/subdir/sub-subdir/
RewriteRule ^(.*)$ https://www.site.com/$1 [R,L]
I get an error where I see the content from my 404 page, but the URL is https://www.site.com/403.shtml - but I don't have a 403.shtml in my web root. (This is a WHM/cPanel-driven server)