1

I'm building a small app in ReactJS, so all pages need to serve index.html and the JS handles the url. This works fine. But I'd also like to have .htaccess remove www from the url if it exists. I'm reading through the mod_rewrite documentation and I can't quite figure out how to make it do both.

Here is my code in .htaccess, please advise!

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(.*) /index.html [NC]
    RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 
</IfModule>

1 Answer 1

2

Answered my own question

<IfModule mod_rewrite.c>
  RewriteEngine On

  # remove www
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
  RewriteRule ^(.*)$ http://%1/$1 [R=301,L,NE] 

  # redirect all to index
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^ index.html [L,NC]
</IfModule>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.