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?

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

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).

Comments

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.

1 Comment

If you look closely at the OP port 80 is not used at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.