Forum Moderators: phranque

Message Too Old, No Replies

How to Redirect https://domain.com/ to https://www.domain.com/

         

tjeni

7:38 am on Mar 1, 2011 (gmt 0)

10+ Year Member



I have this code in my htaccess file as shown below.....

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) [%{HTTP_HOST}%{REQUEST_URI}<...]

This was added to redirect http to https.....so the entire site is now in https version

I need help with redirecting it to a www version from non-www....

When i type mydomain.com i should be redirected to [mydomain.com...] instead of [mydomain.com...]

Right now am landing on [mydomain.com...] when i type mydomain.com

I have tried 301 redirect but its taking me to [mysite.com...]

I don't understand whats the error....

g1smd

9:24 am on Mar 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In your original code, "www" does not appear, you use %{HTTP_HOST} which keeps whatever was requested.

RewriteEngine On

# Redirect ANY non-www request to https and www
RewriteCond %{HTTP_HOST} !^(www.\.example\.com)?$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

# Redirect ANY HTTP request to https and www
RewriteCond %{SERVER_PORT} !443
RewriteRule (.*) https://www.example.com/$1 [R=301,L]


Never mix Redirect/RedirectMatch and RewriteRule directives in the same site. It can lead to weird effects including double redirects and exposing previously rewritten server-internal filepaths back out on to the web as URLs.

If you use RewriteRule for any of your rules, you should use it for ALL of the rules.