【问题标题】:How do I configure my .htaccess file for React App in Subdirectory?如何在子目录中为 React App 配置我的 .htaccess 文件?
【发布时间】:2019-07-18 08:59:35
【问题描述】:

我想将我的 react 应用程序托管在我的共享主机帐户的子目录中。即在目录domain.com/mydirectory/ 中。但是当我在搜索栏中输入 domain.com/mydirectory/ 时,它会打开目录的索引,而不是 React 应用程序本身。

我已经为我的应用创建了构建版本。因此 index.html 文件位于包含静态文件夹的目录的根目录以及在 react-build 脚本期间创建的所有其他内容。

我怎样才能让它正常工作?如何配置我的 .htaccess 文件或我应该考虑什么修复?

【问题讨论】:

    标签: reactjs .htaccess


    【解决方案1】:

    您可以尝试在您的 React 目录中添加一个 .htaccess 文件,其中包含该源代码。

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /subdirectory
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule . /index.html [L]
    </IfModule>
    

    【讨论】:

    • 可能你会得到一个空白页面,因为服务器没有找到你的静态目录修改 index.html 文件链接,如 /static/ 到 /subdirectory/static/。
    • 像魅力一样工作 :D 但不要忘记取消注释 conf 中的模块。参考。 gist.github.com/alexsasharegan/173878f9d67055bfef63449fa7136042
    • 我是前端开发人员。你们有没有机会展示正确的方法并强制使用 SSL?我以前管理过,但每次都必须搜索...谢谢。
    • @MegPhillips91for ssl 你可以使用 certbot
    • @MegPhillips91 您可以尝试使用此命令重定向到 https。 RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ example.com/$1 [R,L]
    【解决方案2】:

    只需将以下内容复制并粘贴到您的.htaccess

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

    【讨论】:

      猜你喜欢
      • 2019-08-31
      • 2020-08-20
      • 2021-05-10
      • 1970-01-01
      • 2016-03-27
      • 1970-01-01
      • 2011-10-14
      • 2018-08-31
      • 2014-10-24
      相关资源
      最近更新 更多