【问题标题】:react-router redirect to index.html AND remove www from url in .htaccessreact-router 重定向到 index.html 并从 .htaccess 中的 url 中删除 www
【发布时间】:2017-01-16 17:08:17
【问题描述】:

我正在 ReactJS 中构建一个小应用程序,因此所有页面都需要提供 index.html 并且 JS 处理 url。这工作正常。但我也想让 .htaccess 从网址中删除 www(如果存在)。我正在阅读 mod_rewrite 文档,但我无法弄清楚如何同时做到这两点。

这是我在.htaccess中的代码,请指教!

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

【问题讨论】:

    标签: .htaccess mod-rewrite reactjs react-router no-www


    【解决方案1】:

    回答了我自己的问题

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

    【讨论】:

      猜你喜欢
      • 2014-01-03
      • 2011-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多