You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel站点.htaccess适配问题:Admin面板与Ionic API无法共存

Solution to Make Laravel Admin Panel & Ionic REST API Work Together

Got it, let’s fix this once and for all—your issue boils down to balancing two critical needs: routing web requests correctly to Laravel’s public directory (for the admin panel) and preserving the Authorization header your Ionic API calls depend on. Here’s a unified .htaccess configuration that handles both:

Final Working .htaccess Code

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Preserve Authorization header for API authentication
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%1]

    # Route all non-public requests to Laravel's public directory
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>

Why This Works

  • Authorization Header Handling: The first rule captures the incoming Authorization header (sent by your Ionic app for API auth) and stores it in an environment variable HTTP_AUTHORIZATION. Laravel relies on this variable to validate API requests—without it, your API calls will fail authentication checks.
  • Public Directory Routing: The second rule acts exactly like your original working config: it redirects any request not already targeting the public folder to public/$1, ensuring your Laravel admin panel routes load correctly.

Quick Validation Steps

  • Make sure mod_rewrite is enabled on your Apache server (most hosting providers have this enabled by default, but if not, you may need to turn it on via cPanel or server config).
  • Confirm your API routes are in Laravel’s routes/api.php (these automatically use the /api prefix, which the .htaccess will forward properly to public/api).
  • Check that your admin panel routes are in routes/web.php—the routing rule will ensure these are served from the public directory as expected.

This setup should let both your admin panel load normally and your Ionic app’s API calls authenticate and succeed without conflicts.

内容的提问来源于stack exchange,提问作者sinaxtis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:02:32