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

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

问题描述

我正在用 ReactJS 构建一个小应用程序,所以所有页面都需要提供 index.html 并且 JS 处理 url.这工作正常.但我也想让 .htaccess 从 URL 中删除 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 中的 url 中删除 www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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