views:

32

answers:

2

we have a url for eg https://www.egdomain.com/ and the ssl is valid for egdomain.com. so how can i redirect all the requests from https://www.egdomain.com/ to https://egdomain.com/

the site also has normal http requests which works fine.

I tried the htaccess below but still nothing

RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]

RewriteCond %{HTTP_PORT} =443

RewriteRule (.*) https://%1/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]

RewriteRule (.*) http://%1/$1 [L,R=301]

Any help will e much appreciated

A: 

The manual says (emphasis mine):

RewriteCond backreferences: These are backreferences of the form %N (1 <= N <= 9), which provide access to the grouped parts (again, in parentheses) of the pattern, from the last matched RewriteCond in the current set of conditions.

So:

RewriteCond %{HTTP_PORT} =443
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule (.*) https://%1/$1 [L,R=301]

Note that if your certificate is only valid for the variant without www., visitors will still get a security warning when accessing the www. variant.

Artefacto
A: 

You can combine these two rules to one:

RewriteCond %{HTTPS}s ^on(s)|
RewriteCond http%1://%{HTTP_HOST}%{REQUEST_URI} ^(https?://)www\.(.+) [NC]
RewriteRule ^ %1%2 [L,R=301]

But maybe this is a little too confusing.

Gumbo