reactjs - 重定向到 protected https 在 react 路由器中不起作用

标签 reactjs .htaccess redirect react-router

create-react-appreact-router 一起使用,我怎样才能做出以下任何变化(我认为我没有遗漏任何变化):

重定向到:

https://www.example.com (注意 https, protected )

我尝试添加 www 的子域,然后使用以下代码创建了一个 .htaccess 文件:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

当我添加它时, react 路线不起作用,我得到一个空白页面。 (我正在使用 1and1 来托管。) 我做错了什么,我该如何解决?

最佳答案

我遇到了这个问题,我也在使用 1&1,

这是因为您需要将所有内容重定向到您的 index.html 以使 react-router 正常工作。

在这里,您只需要将 http 重定向到 https,这是工作的第一部分。
但是,您还需要将该 https 请求重定向到您的 index.html文件。

因此,您将 http 重定向到 https:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NC,L,R=301]

然后,如果 https 处于“打开”状态,您会将所有内容重定向到 index.html

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ /index.html [NC,L,QSA]

你可以在这里测试你的.htaccess:https://htaccess.madewithlove.be/
它在理论上工作正常,但我不知道为什么在我的情况下当 URI 为“/”时重定向不起作用。

所以我添加了这个:

“如果 https 未激活且 URI 为“/”,则使用 https 重定向到我网站的根目录”

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^.$
RewriteRule .* https://"your-site.com"/ [NC,L,R=301]

总结答案

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteCond %{REQUEST_URI} ^.$
    RewriteRule ^(.*)$ https://"your-site.com"/ [NC,L,R=301]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NC,L,R=301]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{HTTPS} on
    RewriteRule ^(.*)$ /index.html [NC,L,QSA]

</IfModule>

关于reactjs - 重定向到 protected https 在 react 路由器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58844423/

相关文章:

javascript - 为什么要执行前一个屏幕组件中的 socket.on() ?

regex - 如何定义重写规则以接受除 a-zA-Z- 之外的空字符串?

php - 无法获取 Wordpress 的 apply_filters ('site_url' ) 以返回 https 方案

reactjs - 如何在componentDidMount中触发子组件事件

javascript - I18N 在 Next.JS 中更改语言

javascript - 我如何在 reactjs 中单击按钮时呈现 .js 文件?

.htaccess - 我如何使用 htaccess 在测试 wamp 服务器上重定向

.htaccess - 访问 https 页面后,所有后续页面都从 https 提供

asp.net-mvc-3 - 登录后MVC3重定向

mod-rewrite - IIS URL 重写 : Redirect not working?