/    Sign up×
Community /Pin to ProfileBookmark

htaccess redirect whole site to https

I am using the below code in htaccess to redirect traffic to https. It work fine if I just type domain.com in the address bar but if I type [url]www.domain.com[/url] it doesn’t get redirected. How do I make it redirect both with and without www?

[CODE]RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
[/CODE]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@DAL_Jun 03.2014 — RegEx is not (and never will be) my strong point but maybe this holds the answer:

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

The above code ensures that the website only ever uses http://www.

Hope it helps
Cheers
Dal
×

Success!

Help @phpnewbie08 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...