Prestashop 1.6本地API经Ngrok访问返回302重定向问题求助
Hey there! Let's break down why you're getting that 302 redirect when accessing your PrestaShop 1.6 REST API via Ngrok—this absolutely ties into PrestaShop's core configuration and request handling logic. Here are the most likely culprits and fixes:
PrestaShop 1.6 is strict about its configured canonical shop URL, stored in Preferences > Shop Parameters > Traffic & SEO > URLs. If your local setup uses localhost:8888 but Ngrok serves a random domain like xyz789.ngrok.io, PrestaShop will automatically redirect requests to the URL it considers "correct".
- Fix: Temporarily update your shop's base URL to the Ngrok domain. You can do this via the back office, or directly edit the
ps_configurationtable in your database—look for thePS_SHOP_DOMAINandPS_SHOP_DOMAIN_SSLentries. Don't forget to switch it back tolocalhost:8888once you're done testing!
PrestaShop's default .htaccess includes rules that enforce the canonical shop URL. If the incoming request's host doesn't match the configured domain, these rules will trigger a 302 redirect.
- Fix: Modify your
.htaccessto allow the Ngrok domain. Look for lines similar to this:
Either comment out these lines temporarily, or update theRewriteCond %{HTTP_HOST} !^localhost:8888$ RewriteRule ^(.*)$ http://localhost:8888/$1 [R=301,L]RewriteCondto include your Ngrok domain (e.g.,!^(localhost:8888|xyz789.ngrok.io)$).
If your custom REST API module includes its own URL validation logic (like checking the request host against the configured shop URL), it might be initiating the redirect.
- Fix: Dig into your module's code. For example, if you're using
Tools::getShopDomain()to build API endpoints, this will pull the configured local URL instead of the incoming Ngrok host. Replace that with$_SERVER['HTTP_HOST']to use the actual request domain for endpoint generation.
Ngrok serves HTTPS URLs by default. If your local PrestaShop is set to use HTTP, or vice versa, this mismatch can trigger a 302 redirect.
- Fix: Align your SSL settings with Ngrok's scheme. If using Ngrok's HTTPS URL, set
PS_SSL_ENABLEDto1in theps_configurationtable (or enable SSL via the back office). Alternatively, use Ngrok's HTTP URL (prefix your Ngrok domain withhttp://instead ofhttps://) if your local setup doesn't use SSL.
Most of the time, this 302 issue stems from PrestaShop's rigid URL canonicalization rules. Adjusting the shop URL, tweaking .htaccess, or updating your module's host handling should resolve the redirects and let your API work via Ngrok smoothly.
内容的提问来源于stack exchange,提问作者juanba1984

