JJump88
JJump88

Reputation:

Apache ssl redirect using mod_rewrite

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?

Upvotes: 4

Views: 6514

Answers (3)

Gumbo
Gumbo

Reputation: 655755

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

Upvotes: 1

Adri&#225;n Navarro
Adri&#225;n Navarro

Reputation: 513

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.

Upvotes: 0

Residuum
Residuum

Reputation: 12064

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

Upvotes: 1

Related Questions