4

I want to do this: if they do https://example.com I want to redirect them to https://www.example.com (add the www.). I have tried oodles of things to no avail.

Redirect https://example.com/<anything> to https://www.example.com/<anything>
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L] 

This code is in httpd.conf but has been tried in .htaccess and ssl.conf.

Can anyone help?

Gumbo
  • 643,351
  • 109
  • 780
  • 844

3 Answers3

1

Have you turned on Rewriting via RewriteEngine On or is mod_rewrite installed? Otherwise, your code should work.

Residuum
  • 11,878
  • 7
  • 40
  • 70
1

The Redirect directive does only work on the URL path. But it’s possible with mod_rewrite. This rule will work in any configuration file:

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

And don’t forget the obligatory RewriteEngine on like (Residuum already said)(1278432#1278432).

sshow
  • 8,820
  • 4
  • 51
  • 82
Gumbo
  • 643,351
  • 109
  • 780
  • 844
0

Use this:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

Of course, don't forget to replace "www.example.com" with your own domain.

Adrián Navarro
  • 503
  • 4
  • 14