react-router重定向到index.html并从.htaccess中的网址中删除www [英] react-router redirect to index.html AND remove www from url in .htaccess

查看:112
本文介绍了react-router重定向到index.html并从.htaccess中的网址中删除www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ReactJS中构建一个小型应用程序,因此所有页面都需要提供index.html,而JS则负责处理url.这很好.但我也想让.htaccess从网址中删除www(如果存在).我正在阅读mod_rewrite文档,但我还不太清楚如何使两者同时实现.

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.

这是我在.htaccess中的代码,请告知!

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>

推荐答案

回答了我自己的问题

<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>

这篇关于react-router重定向到index.html并从.htaccess中的网址中删除www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆