We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

glowcube • 6 months ago

Works like a charm! :)

Alek • 3 years ago

This is great post! There's a small typo in your example after "you can skip that line," and it may lead to some confusion. It shows the negation mark: RewriteCond %{HTTP_HOST} !^www\. [NC], and afterwords you say, "The second condition determines if the request is using the www URL." -- For those words, it would be more clear for the example to be affirmative RewriteCond %{HTTP_HOST} ^www\. [NC] or you could change the paragraph to be negatory... "The second condition determines if the request is not using the www URL."

steamseo.com • 4 years ago

after add this code i checked my website on semrush and find they cant able to crawl my site.

why ?

af • 5 years ago

I can't get: https://www.example.com to forward to: https://example.com

I have http://www.example.com, http://example.com going to https://www.example.com - but just can't figure out the rule and virtual host container to get the www to non-www using https...

Ramanan • 5 years ago

HSTS complicates this. This is because if you need preload with HSTS and are on www, hstspreload.org doesn't like that single redirection and wants http to redirect to https and to www separately. Any thoughts?

Ahmed • 5 years ago

thank you !

Pramil Gawande • 5 years ago

This has worked for me.

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]

Jackalope McAllister • 4 years ago

I don't see any difference between this and the second example in the post.

coRpSE • 4 years ago

There isn't a difference, they are both identical.

Chris Riek • 5 years ago

Thanks. That works for me too. The configuration in the article didn't worked somehow. Nevertheless great help this blog.

Pramil Gawande • 4 years ago

Glad that it worked for you.

Philip Savarirayan • 5 years ago

good!!

4tab • 6 years ago

How to force trailing slash?

Krishna • 5 years ago

RewriteEngine On
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ https://www.domain.com/$1/ [R=301,L]

Harrie van der Lubbe • 6 years ago

I think you can explain where in your conf file this should reside - how you defined you VirtualHost. For instance I have the virtualhosts separated between port 80 and port 443 (it's how certbot likes it I believe).

<virtualhost *:80="">

vs

<virtualhost *:443="">

if you try to rewrite from https://www to https:// (without www) in the virtualhost listening on port 80, it won't work. Because it will never get there (it's not https there). That rewrite rule should be in the virtualhost listening on port 443 - or make sure your virtualhost listens to any port, of course (I guess this is what was the case in your setup).

This cost me an hour or two, before I figured it out. Hope this helps others.

amihaiemil • 4 years ago

Thank you! Lord, Jesus Christ Allmighty, I had wasted about 3 hours on this and was starting to lose it...

Shafeeque M • 5 years ago

Below rules helped me to redirect https://www to https:// (without www)

<virtualhost *:80="">
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
</virtualhost>

<virtualhost *:443="">
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
</virtualhost>

Abby Buzon • 6 years ago

THANK YOU! What an AWESOME resource!
The "standard" redirects put in place by hosts, or mentioned in other articles, will often cause multiple unnecessary redirects - straight URL redirects to www (non https) then to https://www. Or worse, if it's a non-www, because of CDN requirements, I've seen sites redirect to www, then to https, then to non-www https!

Bijay • 6 years ago

Hi, your code works great but it doesn't redirect https://www to https:// version. I tried to modify the code but haven't succeeded. Please help.

Quasar • 4 years ago

I had this problem too. The issue with redirecting from https://www.example.com to https://example.com is that you need the SSL certificate for www.example.com, because that will be checked *before* the redirect. Here is the solution that worked for me, and which does not require any rewrite rules. This is the entire content of /etc/apache2/sites-available/mydomain.conf:

# 1. Redirect from http://mydomain.org/ to https://mydomain.org/
<virtualhost *:80="">
ServerName mydomain.org
Redirect / https://mydomain.org/
</virtualhost>

# 2. Redirect from http://www.mydomain.org/ to https://mydomain.org/
<virtualhost *:80="">
ServerName www.mydomain.org
Redirect / https://mydomain.org/
</virtualhost>

# 3. Redirect from https://www.mydomain.org/ to https://mydomain.org/
<virtualhost *:443="">
ServerName www.mydomain.org
Redirect / https://mydomain.org/

# Note: we need to supply the SSL certificate for www.mydomain.org,
# because this will be checked *before* the redirect.
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/www.mydomain.org/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.mydomain.org/privkey.pem
</virtualhost>

# 4. Virtual host configuration for https://mydomain.org/
<virtualhost *:443="">
ServerName mydomain.org
ServerAdmin webmaster@mydomain.org
DocumentRoot /var/www/mydomain

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/mydomain.org/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.org/privkey.pem
</virtualhost>

John Gille • 6 years ago

This code worked great for me except for one scenario.
I'm trying to redirect all variations of a domain to https://www.example.com, and everything is working, except for when I go to https://example.com without the www, it generates an SSL certificate error saying the site isn't secure. I can click a link that says "Proceed to this site anyways" and the site still loads, but would like to avoid this error. After I click the link to Proceed to this site anyways, the redirect happens successfully. Is this something I can fix with a redirect, without purchasing a separate SSL certificate for both www and non www versions?

augusto_fagioli • 6 years ago

Eccellente!
Grazie

IPTV Link Lister • 6 years ago

This is by far the best organized and most easily understood introduction to htaccess rules I've ever seen. I also wanted to mention that using your third RewriteCond with %1 in the RewriteRule is great for those using localhost development before pushing to a production site.

Bigjam • 6 years ago

Hi sorry to be a noob but my .htaccess file already had some code in it (see below) so where would I put your code in here?

# BEGIN WordPress
<ifmodule mod_rewrite.c="">
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</ifmodule>
# END WordPress

Or, where would I put this code in my apache2.conf file? There's so much code!

Gaurav Guru • 6 years ago

This is a great resource.. worked for me!

Carlo • 6 years ago

Thanks for this article. What if a visitor writes https://www.example.com in the browser address bar? How is the RewriteCond formatted in the .htaccess file so that the server redirects to https://example.com? This seems to be the only combination not covered by your example and the one I am having a problem with. Thanks!

Tihomir Valkanov • 6 years ago

Very nice! Thanks!

Worked for me! Thanks!

c0b1 • 7 years ago

Good job mate!

Jon • 7 years ago

This doesn't work for images that are accessed directly.

Divyanshu Sharma • 7 years ago

Hi, Thanks for the above tutorial. I have made my web Application in react and Nodejs. I am hosting it as subdomain on aws. And i have got my domain registered by Plesk. After applying SSL on load balancer . I am able to redirect my website from http to https, so my website is accessible when i type my_subdomain.com, www.my_subdomain.com, https://www.my_subdomain.com/. But its showing timeout error when i type "https://my_subdomain.com/". I found it very strange that its redirecting from my_subdomain.com to https://my_subdomain.com/, but not running https://my_subdomain.com/ directly. Do anyone has some idea about the possible issue.

I am using the rewrite rule below to redirect from http to https.

<configuration>
<system.webserver>
<rewrite>
<rules>
<rule name="http to https at lb" enabled="true" patternsyntax="Wildcard" stopprocessing="true">
<match url="*" ignorecase="false"/>
<conditions logicalgrouping="MatchAny">
<add input="{HTTP_X_Forwarded_Proto}" pattern="http"/>
<add input="{HTTPS_X_Forwarded_Proto}" pattern="https"/>
<add input="{HTTP_HOST}" pattern="subdomain.domain.com"/>
</conditions>
<action type="Redirect" url="https://www.subdomain.domai...{REQUEST_URI}"/>
</rule>
</rules>
</rewrite>
<httpredirect httpresponsestatus="Permanent"/>
</system.webserver>
</configuration>

I don't know what is the mistake i am doing

Bruno Oliveira • 7 years ago

Thank you for your tutorial. Helped me a lot!

Vishal Patel • 7 years ago

Its is not Working also in CI

Hamid Raza • 7 years ago

it doesn't work for safari and MS edge Why any one can explain ??

Peter Gallagher • 5 years ago

I've been experiencing the same issue with safari. The redirect works fine in chromium browsers.

Kalpesh Popat • 7 years ago

no matter what i try, it doesnt work for Safari and Firefox. Chrome is ok.

codigo.bet • 7 years ago

P-E-R-F-E-C-T Thanks

Peter Osterversnik • 7 years ago

Very nice tutorial. Thank you! I however have a problem that when I enter www. before the domain name in the browser, the server will then add another www. and I will get two: www.www in front of domain name. How do I prevent that?

Jason Smyth • 7 years ago

Hi Simone,
I think I'm running into conflicts while I try to drop file extensions off my URL's.

I may be naive in the use of my expressions, any advice on the following?
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

#remove php file extension-e.g. https://example.com/file.php will become https://example.com/file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

Flammm • 7 years ago
Shahriar • 7 years ago

Thanks a lot ^_^ It helped very very much. I have been looking for this solution for about 3 days. And I finally found the proper one.

Eileen Callahan • 7 years ago

Such a helpful article, Simone! You spelled it all out so clearly - many other articles were VERY confusing! Thank you!!

Vu Quang Anh NGUYEN • 7 years ago

Thanks you very much. This helps me.

Jon • 7 years ago

I assume the non-www version will work properly for subdomains?

Torbjörn • 7 years ago

THANKS !!
Worked perfect, Simone :-)

dooing • 7 years ago

Great solution! I have one more question. What if you want to exclude a specific sub-domain?

Asif Iqbal • 7 years ago

Great, Clean and Neat solution. [OR] is a perfect word for synchronize rules.

Alexander Kim • 7 years ago

You forgot about https://www.domain.con redirect to https://domain.com

Sai Charan • 7 years ago
Jeppe Awesome Refsgaard • 7 years ago

What if i type in https://www.example.com and want it to redirected to https://example.com
On my website, that gives me an error "This site can’t be reached"