3

Probably I'm overseeing something very basic, htaccess is not my field of expertise.

My site is https-only, to achieve this, the root folder / contains these lines:

RewriteEngine On
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [R=301,L]

This works just fine. Now we have a new folder called /projects, which contains its own htaccess rules to translate the url in a database query by a file called /projects/project.html

The working htaccess within the folder /projects is:

RewriteEngine On 
RewriteBase /projects/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ project.html?project=$1 [QSA,L]

Now I would like to force https connection on this one, because the https rule in the main directory is not applied on /projects if you visit by http://URL directly. Therefore I modified the file to:

RewriteEngine On 

RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://example.com%{REQUEST_URI} [R=301,L,NE]

RewriteBase /projects/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ project.html?project=$1 [QSA,L]

With this, it switches to https, but I keep getting "ERR_TOO_MANY_REDIRECTS".

When I change the lines as @thickguru suggested, like this:

RewriteRule ^(.*)$ https://example.com/$1 [R,L]

It will redirect me to https://example.com/index.html right away.

I fiddled around with the settings, also with the information of this post, however still I can't get it to work.

SJDS
  • 312
  • 7
  • 19
  • What version of apache are you using? – Amit Verma Feb 09 '17 at 05:07
  • I'm not sure if I'm giving the correct answer, but phpinfo mentions Apache2, API version 20051115. If I need to look somewhere else, please let me know :) – SJDS Feb 09 '17 at 08:02

2 Answers2

3

I just noticed it the variable you are matching against is %{ENV:HTTPS} , so , you can use the following rule in /projects/.htaccess :

RewriteEngine on

RewriteCond %{ENV:HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]

Clear your browser cache or try a diffrent browser to test this.

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • At least now page loads correctly when using https:// in the URL, with the complete code now being `RewriteEngine On RewriteCond %{ENV:HTTPS} !on RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R] RewriteBase /projects/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)$ project.html?project=$1 [QSA,L]` I now get ERR_TOO_MANY_REDIRECTS when typing in the http version of the URL. – SJDS Feb 09 '17 at 07:59
  • Sorry, there was a typo in Redirect destination. Please see the edit – Amit Verma Feb 09 '17 at 08:05
  • 2
    Aaah now you point it out, that typo was obvious, stupid I didn't see. This is exactly the solution I was looking for. Thanks a lot every one who contributed. Special thanks to @tickguru for the bounty! – SJDS Feb 09 '17 at 10:29
  • @starkeen what is `%{ENV:HTTPS}` I mean how it is different from `%{HTTPS}`? – Abhishek Gurjar Feb 09 '17 at 12:15
  • @AbhishekGurjar Unlike **{HTTPS}** , **{ENV:HTTPS}** is a user defined variable. You can set it using ENV or SETEnv directive or it can also be set using mod rewrite's E flag. – Amit Verma Feb 09 '17 at 12:56
1

Use this in your .htaccess:

RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://www.example.com%{REQUEST_URI} [R=301,L,NE]

Also, place this above your your other RewriteRule, so that your .htaccess will look like:

RewriteEngine On 

RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://www.example.com%{REQUEST_URI} [R=301,L,NE]

RewriteBase /projecten/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ project.html?project=$1 [QSA,L]

EDIT - Try this:

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Joe
  • 4,877
  • 5
  • 30
  • 51
  • This does not work, gives error that request will never end.. In address bar the URL has changed to HTTPS variant though – SJDS Jan 24 '17 at 09:10
  • I've made edits to the code. Please try again with new code. Also, test it with it being below the other `RewriteRule` also. – Joe Jan 24 '17 at 09:13
  • I tried it in both orders, plus tried playing with the R=301, L, NE and QSA tags. However, keeps throwing errors. No correct handling of existing rules in combination with HTTPS – SJDS Jan 24 '17 at 09:21
  • This is still an issue. Any other suggestion? :) – SJDS Feb 08 '17 at 01:54
  • I've made an edit for you to try @TijnvW. Let me know. – Joe Feb 08 '17 at 10:17
  • Aarrrggh this is driving me crazy! Thanks for helping again @thickguru, but unfortunately with `RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]` it redirects me to the homepage https://example.com. With `RewriteRule ^(.*)$ https://www.example.com%{REQUEST_URI} [R,L]` I get: error request will never end. – SJDS Feb 08 '17 at 11:50
  • 2
    Sorry I couldn't help. I've started a bounty for you. – Joe Feb 08 '17 at 12:10
  • Thanks for your effort mate! – SJDS Feb 08 '17 at 12:31
  • @tijnvw Clear your browser cache. The rule is ok and it should work. – Amit Verma Feb 08 '17 at 18:51
  • @starkeen That's not the problem/solution, I clear cache before every page reload. However, I think it may have something to do with conflicting rules in an other directory, I will update my question. – SJDS Feb 09 '17 at 00:03