Problem removing www with https

I want to remove the www from the front of the url request and want it to go to http if requested from http and https if requested from https.

Currently I am using


RewriteCond %{HTTPS_HOST} ^www\\.mydomain\\.com$ [NC]
RewriteRule .? https://mydomain.com%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^www\\.mydomain\\.com$ [NC]
RewriteRule .? http://mydomain.com%{REQUEST_URI} [L,R=301]

but this seems to direct all to http://mydomain.com.

I would have thought that it would evaluate https://www.mydomain.com/dir to https://mydomain.com/dir which then would bypass the second rule because there is no www. Where is my logic going wrong? Thanks

Just a clarification, if I use the following code by itself


RewriteCond %{HTTP_HOST} ^www\\.mydomain\\.com$ [NC]
RewriteRule .? http://mydomain.com%{REQUEST_URI} [L,R=301]

it redirects both http://www.mydomain.com/dir and https://www.mydomain.com/dir to http://mydomain.com/dir

Try this:


RewriteCond %{HTTP_HOST} ^www\\.mydomain\\.com$ [NC]
RewriteCond %{SERVER_PORT} ^443(s)
RewriteRule .? http%1://mydomain.com%{REQUEST_URI} [L,R=301]

(not tested)

bost,

To remove www from an https request, you must check that it is an https request first, i.e.,

RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{HTTP_HOST} ^www\\.example\\.com$ [NC]
RewriteRule .? https://example.com%{REQUEST_URI} [R=301,L]

What’s wrong with your code is:

  1. You didn’t check that the requests were to port 80 or port 443.

  2. You didn’t use the correct code, i.e., {HTTPS_HOST}. If you were trying for {HTTPS}, it’s either NULL/UNDEFINED or ‘on’ (without the quotes).

If you need more sample code, have a read of the forum’s Sticky Post or the tutorial Article linked in my signature.

Regards,

DK

Okay, I received notification of ScallioXTX’s post and tried that and it appeared to do nothing. However, I did some more research on the port concept and came up with the following. When I came in to post this, I saw the dklynn post. Here is what I came up with and it seems to work. It is the same as the dklynn post except for the + vs. the ^ in front of the 443 and 80. Are these interchangeable?


RewriteCond %{SERVER_PORT} =443
RewriteCond %{HTTP_HOST} ^www\\.mydomain\\.com$ [NC]
RewriteRule .? https://mydomain.com%{REQUEST_URI} [L,R=301]

RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} ^www\\.mydomain\\.com$ [NC]
RewriteRule .? http://mydomain.com%{REQUEST_URI} [L,R=301]

I am assuming that the = is implied when I use the anchor ^, is that true? Or the ^ anchor is implied when I use the =.

One more question, does anything ever come in on ports other than 80 and 443?

bost,

Rémon’s didn’t work because of the (s) after 443. I don’t know of any port which includes a character.

Yes. http uses port 80, https uses 443, FTP uses 21, etc.

Are you asking for more help without trying the code I provided?

Regards,

DK

No. Actually both the one you sent and the one I came up with are very similar and both work fine. My only question is on the difference between using the =80 vs. ^80$

They both seem to work equally well so my question is is one preferred over the other and is =80 actually the same as ^80$

=80 and ^80$ are not the same in the way they work, but in this case the end result of both is indeed exactly the same. I’d prefer =80 for this case, since using regex (which is what you do with ^80$) seems a bit overkill for this relatively simple comparison.

Got it, thanks so much for the input.