Welcome to WebmasterWorld Guest from 34.204.191.31

Forum Moderators: Ocean10000 & phranque

Redirection to https://www.

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

Preferred Member

10+ Year Member

joined:Nov 26, 2004
posts: 412
votes: 0


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]

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

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11846
votes: 242


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]

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

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 328
votes: 24



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. (?)
5:25 pm on Mar 30, 2019 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 26, 2004
posts: 412
votes: 0


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]

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

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15893
votes: 876


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.
10:00 pm on Mar 30, 2019 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11846
votes: 242


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]

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

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 328
votes: 24


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).
6:06 pm on Mar 31, 2019 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15893
votes: 876


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