Redirect all traffic to https

adam_g

Expert Member
Joined
Aug 18, 2011
Messages
1,199
Reaction score
264
Location
127.0.0.1
Hi all

i need some help with htaccess

at the moment if i go example.com my website goes to https://example.com this works fine

But when i go www.example.com or https://www.example.com it cant display the webpage

How do i redirect www.example.com and https://www.example.com to https://example.com in htaccess

Ive tried looking on stackoverflow but i cant find anything.

If anyone could help :)

You don't mention what webserver you're using?

Apache? Nginx? Both have different formatting.

Always provide more info as people can then help easier...

Also I suspect this may not be a rewrite rule but a problem with the way you've setup your DNS for your domain. I suspect you've possibly forgotten to add DNS records as follows:

Code:
[url]www.example.com[/url]	14400	IN	CNAME	example.com
example.com	14400	IN	A	your_hosting_ip

It would then work as follows:

Code:
browser > example.com > YourHostingServer
browser > [url]www.example.com[/url] > example.com > YourHostingServer

Anyhow, that aside if it is a .htaccess issue on Apache:

To force all web traffic to use HTTPS insert the following lines of code in the .htaccess file in your website’s root folder.

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

To force a specific domain to use HTTPS, use the following lines of code in the .htaccess file in your website's root folder:

Code:
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X