udgru
udgru

Reputation: 1367

htaccess - How to force http and non-www

I need to set a htaccess domain configuration where https redirects to http and all www requests to non-www. I thought I could solve it with the following code but it does not work correctly. It only redirects http://www.example.com to http://example.com. https requests end up at e.g. https://example.com with the hint that the connection is insecure.

<IfModule mod_rewrite.c>

RewriteEngine On 
RewriteBase /

# Redirect HTTPS to HTTP
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Force non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

</IfModule>

Upvotes: 0

Views: 584

Answers (2)

Ciesel
Ciesel

Reputation: 1

This worked for me.

RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^(.*)$ http://example.com:80/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://example.com:80/$1 

Upvotes: 0

Bjartur Thorlacius
Bjartur Thorlacius

Reputation: 804

Most likely, your shared host proxies all HTTP connections to your site, and forwards them over HTTPS. You never see the connection from the client, only the connection from your shared host proxy. Ask the shared host whether she does or will signal the information you need in HTTP headers such as Forwarded-SSL (or X-Forwarded-SSL).

Upvotes: 0

Related Questions