菜鸟教程小白 发表于 2022-12-11 19:42:21

php - 使用 Alamofire 请求 Web api 时出现重定向错误 301 和 302


                                            <p><p>最近我购买了 <code>SSL</code>,并使用 <code> 将服务器上的所有请求从 <code>http</code> 重定向到 <code>https</code>。 htaccess</code></p>

<pre><code>RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI}
</code></pre>

<p>但是,我的 <code>iOS</code> 和 <code>Android</code> 应用程序已经从 <code>Alamofire</code> 网络库中请求来自 <code>http</code> 的服务,所以它显示以下错误</p>

<pre><code>&lt;!DOCTYPE HTML PUBLIC &#34;-//IETF//DTD HTML 2.0//EN&#34;&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;302 Found&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;Found&lt;/h1&gt;
    &lt;p&gt;The document has moved
      &lt;a href=&#34;https://api.example.com/webservices/v1/services.php&#34;&gt;here&lt;/a&gt;.
    &lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>我知道 301 和 302 是重定向错误,但网络服务显示来自浏览器的正确数据。来自应用程序的请求不起作用。</p>

<p>即使发生重定向也能接收正确 JSON 数据的任何解决方案。</p>

<p>请提出建议。任何帮助表示赞赏。</p>

<p>PS:还有其他原因我无法在应用程序中更改请求。 </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>试试这个:</p>

<pre><code>RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</code></pre>

<p>这会将 <code>301</code> 重定向添加到 <code>.htaccess</code>。</p>

<p>所以,完整的代码是:</p>

<pre><code>RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于php - 使用 Alamofire 请求 Web api 时出现重定向错误 301 和 302,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/48545840/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/48545840/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: php - 使用 Alamofire 请求 Web api 时出现重定向错误 301 和 302