Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Apache .htaccess SSL React Router
<ifModule mod_rewrite.c>
#######################################################################
# GENERAL #
#######################################################################
# Make apache follow sym links to files
Options +FollowSymLinks
# If somebody opens a folder, hide all files from the resulting folder list
IndexIgnore */*
#######################################################################
# REWRITING #
#######################################################################
# Enable rewriting
RewriteEngine On
# If its not HTTPS
RewriteCond %{HTTPS} off
# Comment out the RewriteCond above, and uncomment the RewriteCond below if you're using a load balancer (e.g. CloudFlare) for SSL
# RewriteCond %{HTTP:X-Forwarded-Proto} !https
# Redirect to the same URL with https://, ignoring all further rules if this one is in effect
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [R,L]
# If we get to here, it means we are on https://
# If the file with the specified name in the browser doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f
# and the directory with the specified name in the browser doesn't exist
RewriteCond %{REQUEST_FILENAME} !-d
# and we are not opening the root already (otherwise we get a redirect loop)
RewriteCond %{REQUEST_FILENAME} !\/$
# Rewrite all requests to the root
RewriteRule ^(.*) /
</ifModule>
<IfModule mod_headers.c>
# Do not cache sw.js, required for offline-first updates.
<FilesMatch "sw\.js$">
Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
</FilesMatch>
</IfModule>
@seksitha

This comment has been minimized.

Copy link

@seksitha seksitha commented Mar 15, 2019

Thank you very much!!! save my day! and learn a lot from this!!!

@steve-cahn

This comment has been minimized.

Copy link

@steve-cahn steve-cahn commented Nov 13, 2019

Thank you! Just spent the past 4 hours trying to get this!!! Beautiful!

Just one question... How can I redirect a user from www to without the www, so from www.sample.com to https://sample.com?

@yourPixel

This comment has been minimized.

Copy link

@yourPixel yourPixel commented Mar 3, 2020

Thank you very much! I spent a lot of time to find the answer and it's here !

@copperfox777

This comment has been minimized.

Copy link

@copperfox777 copperfox777 commented Apr 4, 2020

Thank you man

@kurtwilliam

This comment has been minimized.

Copy link

@kurtwilliam kurtwilliam commented May 10, 2020

Saved me, thanks!!

@BranigansLaw

This comment has been minimized.

Copy link

@BranigansLaw BranigansLaw commented May 17, 2020

This is amazing! Saved me so much time!

I would also like to remove the "www" from urls and redirect to the non-www counterpart. I'm pretty green with .htaccess, so I'm sure I'm doing something wrong. I tried this and it's not working:

...
  # Enable rewriting
  RewriteEngine On

  #www to non
  RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?domain\.com)$ [NC]
  RewriteRule ^(.+?)/?$ http://%1/$1/ [R=301,L]

  # If its not HTTPS
  RewriteCond %{HTTPS} off
...
@12aptor

This comment has been minimized.

Copy link

@12aptor 12aptor commented May 17, 2021

Thanks. You are so good

@micaelomota

This comment has been minimized.

Copy link

@micaelomota micaelomota commented Jul 31, 2021

For people that serve the react app in a subdirectory, here is the solution:
Comment out or remove out these two lines bellow

    # Rewrite all requests to the root
    # RewriteRule ^(.*) /

Add this rewrite rule to point to index.html

    # Rewrite all requests to the index.html
    RewriteRule ^ index.html [QSA,L]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment