RewriteRule with greedy matches trying to redirect root to a page, and rewrite all other requests to https - apache

I am trying to work with apache2 rewrite to perform two functions
rewrite (or redirect) / (root) requests to a specific page: /index
all requests (including / and /index) on port 80, are rewritten to HTTPS
Here is my failed attempt
RewriteEngine On
RewriteRule ^$ https://%{HTTP_HOST}%/index [R]
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
My guess is that the last rule was being too aggressive/greedy (everything is rewriting to https) - so I tried to prevent the root match from getting there by adding a RewriteCond that matches on not root, but it is not working.
Edit: is my approach wrong? Is there an obvious mistake in Rules/Conditions?

RewriteCond %{REQUEST_URI} !^$
This will always match, %{REQUEST_URI} does not have the current per-directory prefix removed from it like the implicit thing you match a per-directory RewriteRule against. IOW add the slash in.

For completeness here is the final configuration:
RewriteEngine On
RewriteRule ^$ https://%{HTTP_HOST}%/index
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

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]

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]

.htaccess redirect all tld's to another and force https

Hello i try to redirect all my top level domains to another one and this is already working but i also want to force https. With the following code it is working fine for all top level domains such as '.de', '.net' and so on.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*) https://domain.com/$1 [R=301,NE,L]
My problem is i also want to force https for my main tld => '.com'
Well, and here we are:
RewriteEngine On
RewriteCond (http://) !^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*) https://domain.com/$1 [R=301,NE,L]
For my bad, this is not working. I get an 'Too many redirects' error. My question is now is it the code/redirection or is there something else wrong maybe with the server configuration of my provider? I noticed the 'Too many redirects' error in any way i tried till now for redirect from 'http://' to 'https://' and i tried so many different ways... .
Try:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*) https://domain.com/$1 [R=301,NE,L]
Adds an "or" condition so that it redirects if there's no HTTPS OR if the domain is different.
The (http://) isn't a variable, it's a literal "http://", which will obviously never match ^(www\.)?domain\.com$, which is why that condition is always true.
Since you're using cloudflare, which internally routes requests (that are not HTTPS), you can't use the %{HTTPS} variable, you need to look at the forwarding protocol:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*) https://domain.com/$1 [R=301,NE,L]

Can't get htaccess rule to work: force SSL on all pages, force non-SSL on two specific pages

I am no htaccess expert, but after Googling for two hours I gave up. Maybe you can help me?
I have my entire site on SSL. However, I have two pages that reference non-secure dynamic content from elsewhere. I need these to be on http instead of https.
The first part of my rules work. All the site is forced to SSL except for those two pages. However, the last part doesn't: force those two pages to non-SSL. It is probably very stupid but does anyone see where I go wrong?
#add www. if missing WORKS
RewriteEngine on
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
#force SSL/https WORKS
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/webshop2/localize\.php
RewriteCond %{REQUEST_URI} !^/webshop2/layoutstripper\.php
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#force http DOES NOT WORK
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/webshop2/localize\.php [NC]
RewriteCond %{REQUEST_URI} ^/webshop2/layoutstripper\.php [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You need an [OR] flag in your second SSL rule. The 2 conditions you have essentially say:
the request must be HTTPS
the URI must start with: /webshop2/localize.php
the URI must start with: /webshop2/layoutstripper.php
As you can see, the last 2 conditions will always fail, as the request can't be BOTH at the same time. If you add an [OR] flag in there, it makes it true if the URI is one or the other:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/webshop2/localize\.php [NC,OR]
RewriteCond %{REQUEST_URI} ^/webshop2/layoutstripper\.php [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]