react-router redirect to index.html AND remove www from url in .htaccess

扶醉桌前 提交于 2019-12-02 11:43:29

问题


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:


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>


来源:https://stackoverflow.com/questions/39396488/react-router-redirect-to-index-html-and-remove-www-from-url-in-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!