Forum Moderators: phranque

Message Too Old, No Replies

Redirection to https://www.

         

Dexie

7:33 am on Mar 30, 2019 (gmt 0)

10+ Year Member



Hi all, I would like to make a redirection to https://www.
Which means that:
http:// --> https://www.
http://www. --> https://www.
https:// --> https://www.
https://www. --> no redirection

in the .htaccess file, I have:
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/$1 [L]

But this gives a 500 error. :-(

Any help much appreciated.

Thanks.

Dexie

[edited by: phranque at 9:33 am (utc) on Mar 30, 2019]
[edit reason] unlinked urls [/edit]

phranque

9:43 am on Mar 30, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



But this gives a 500 error

you should check the server error log file for clues.

RewriteCond %{HTTP:X-Forwarded-Proto} =http

are you using a proxy server in your configuration?

in the .htaccess file, I have:
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/$1 [L]


i would try starting with something like this:
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC,OR]
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/$1 [L]

[edited by: phranque at 2:20 pm (utc) on Oct 17, 2019]

whitespace

3:55 pm on Mar 30, 2019 (gmt 0)

10+ Year Member Top Contributors Of The Month




RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/$1 [L]


Without knowing your system, this alone could cause a rewrite loop - which will result in a 500 error being returned to the client. This is probably dependent on another .htaccess file being present in the "web" subdirectory that ultimately routes the URL. (?)

Dexie

5:25 pm on Mar 30, 2019 (gmt 0)

10+ Year Member



Thanks for the input on this. There's no other .htaccess file and as far as I know, ther's no proxy involved either.

Thanks also for the example code, it is to be inputted into many websites, so do you know the code for a generic redirect please, so that whatever anyone put before actual domain name, it would still:
http:// --> https://www. 
http://www. --> https://www.
https:// --> https://www.
https://www. --> no redirection

[edited by: phranque at 9:58 pm (utc) on Mar 30, 2019]
[edit reason] unlinked urls [/edit]

lucy24

6:57 pm on Mar 30, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]
You cannot use %1 when there are OR-delimited conditions, unless every set of conditions has the same capture in the same location. If the first condition is met, there will be nothing to capture. This, by itself, wouldn’t cause a 500 error--but it would in some cases lead to the visitor making an invalid request, which is just as bad.

What’s the NE flag for? Normally this is only needed if the redirect target contains a non-alphabetic character like # that would be escaped by default.

it would still:
It is a very, very, very good idea to look at a post after clicking Submit. If what you see is not exactly what you typed, figure out how to change it. (You have, I think, 60 minutes to edit.) Very often it is sufficient to put the troublesome part into [ code ] markup.

phranque

10:00 pm on Mar 30, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



do you know the code for a generic redirect please, so that whatever anyone put before actual domain name, it would still:
http:// --> https://www. 
http://www. --> https://www.
https:// --> https://www.
https://www. --> no redirection

i would try starting with something like this:
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC,OR]
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

[edited by: phranque at 2:19 pm (utc) on Oct 17, 2019]

whitespace

4:34 pm on Mar 31, 2019 (gmt 0)

10+ Year Member Top Contributors Of The Month



lucy24: You cannot use %1 when there are OR-delimited conditions...


But the last condition is not OR'd and is always met (providing the previous conditions are satisfied).

lucy24

6:06 pm on Mar 31, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Touché. (I think I had a brain fart involving the grouping of multiple ANDs and ORs.)