【问题标题】:React change default route for static filesReact 更改静态文件的默认路由
【发布时间】:2021-10-23 16:00:05
【问题描述】:

我正在尝试在 IIS 上使用 react 托管 .net 核心应用程序,但我遇到了问题。

IIS 找到 index.html,但是当响应 tris 以相对路径获取其他文件时,简而言之, URL http://localhost/BonAppetit 上的反应应用程序尝试从示例加载文件 http://localhost/react/static/css/main.6faa2c66.chunk.css 女巫返回 404,因为文件实际上在 http://localhost/Deploy-Test/static/css/2.ed890e5e.chunk.css 上。

我在这里看到两个选项

  1. 在域上托管应用,以便相对路径起作用
  2. 在 react 中配置静态文件的默认路径

我不知道怎么做。

【问题讨论】:

  • 关于option1,我很好奇你是怎么部署的?它被部署为某个站点的子应用程序?您可以关注this 将其部署在独立站点上。我不是反应方面的专家,所以不能对选项2提供任何有用的建议。

标签: reactjs iis hosting single-page-application


【解决方案1】:

您好,您必须创建一个 .hhtaccess 文件:

    Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

如果您使用 apache,则在您的主机服务器上 在 /etc/apache2/sites-available 上创建一个文件 yourdomain.com.conf 在这个文件上:

<IfModule mod_rewrite.c>
    <Directory "/var/www/html/yourdomain.com">
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.html$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-l
        RewriteRule . /index.html [L]
    </Directory>
 </IfModule>
<VirtualHost *:80>
    ServerName yourdomain.com
DocumentRoot "/var/www/html/yourdomain.com"
RewriteEngine on
RewriteCond %{SERVER_NAME} =yourdomain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

【讨论】:

  • 请注意.htaccess 文件将位于public 文件夹中。然后它将在构建时复制到build 文件夹中。 See more information about serving React apps this way.
  • 你确定吗?我使用的是 IIS 而不是 apache。
  • 好的,所以在你的构建中尝试使用.htacces ``Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.html [QSA,L] ``
猜你喜欢
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
  • 2012-03-17
  • 1970-01-01
  • 2017-09-13
  • 1970-01-01
  • 2019-07-21
  • 1970-01-01
相关资源
最近更新 更多