user3355434
user3355434

Reputation: 92

apache .htaccess rewrite should not include www

We have a domain test.com, we want to redirect http://www.test.com to http://test.com and https://www.test.com to https://test.com, We can achieve the same by below mentioned rule, but we dont want http:// and beside what do we have to do for https://

 RewriteEngine On
 RewriteBase /
 RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
 RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Upvotes: 2

Views: 85

Answers (2)

anubhava
anubhava

Reputation: 785146

You can have a single rule to handle both http and https traffic:

RewriteEngine On
RewriteCond %{HTTP_HOST}#%{HTTPS}s ^www\.([^#]+)#(?:off|on(s)) [NC]
RewriteRule ^ http%2://%1%{REQUEST_URI} [R=302,L,NE]

Upvotes: 1

Jon Lin
Jon Lin

Reputation: 143886

If you want everything to go to https, then you need to change your rule to:

 RewriteEngine On
 RewriteBase /
 RewriteCond %{HTTPS} off [OR]
 RewriteCond %{HTTP_HOST} ^www\.test\.com$ [NC]
 RewriteRule ^(.*)$ https://test.com/$1 [R=301,L]

Otherwise, you'll need separate rules for http and https:

 RewriteEngine On
 RewriteBase /
 RewriteCond %{HTTPS} off
 RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
 RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

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

Upvotes: 1

Related Questions