G
G
GaserV2017-05-12 19:39:22
htaccess
GaserV, 2017-05-12 19:39:22

Why did the CORS problem appear?

Hi all! Such a disaster. Initially installed CORS middleware. and added headers to htaccess. Everything worked on a "normal" controller, so to speak. After I decided to optimize and created a resource controller. On LAN, everything worked out the rules, and after, when the transfer to the host, there was a problem with CORS. I can't send a POST request while GET works fine..

<?php

namespace App\Http\Middleware;

use Closure;

class Cors {
    public function handle($request, Closure $next)
    {
        return $next($request)
            ->header('Access-Control-Allow-Origin', 'http://app.bezraboty.by')
            ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    }
}

<IfModule mod_rewrite.c>
    RewriteEngine On

    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
    Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Boris Korobkov, 2017-05-12
@BorisKorobkov

Everything worked fine locally

And locally on different domains tested? Looks like no.
1. Why do you duplicate headers from both Apache and PHP? One is enough (PHP is better)
2. Only one Access-Control-Allow-Origin is enough without the rest
3. Check the returned headers from /cors.txt . At the same time, make sure that they are served immediately, and not redirected to /cors.txt/ .
4. Check your algorithm at https://upload.wikimedia.org/wikipedia/commons/thu...

G
GaserV, 2017-05-13
@GaserV

In general, it was necessary to modify htaccess a little)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question