mod_rewrite just broke Server-sent Event content-type?

369 Views Asked by At

I have the following rules in my .htaccess file:

Options +FollowSymLinks 
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(?:\.\w+)$

RewriteRule ^func/(.*)(/.*)?$ includes/function/post/$1.php$2

So I can just visit:

http://localhost/func/create/?ID=1
(it'll goto http://localhost/includes/function/post/create.php?ID=1)

It worked just fine, but if I using EventSource in javascript like this:

new EventSource('http://localhost/func/create/?ID=' + ID);

then it'll add Accept: text/event-stream into the request header,

but I noticed the Content-Type of the response header was text/html; charset=iso-8859-1,

then just throw me a HTTP 406 Not Acceptable status code,

(work fine if I remove Accept: text/event-stream in request header)

and I make sure that I added this line in my php source code:

header('Content-Type: text/event-stream;');

If I visit http://localhost/includes/function/post/create.php?ID=1,

everything worked just fine, Is EventSource not supported mod_rewrite?

EDIT ---

I tried:

<FilesMatch "create.*$">
    ForceType text/event-stream
</FilesMatch>

but it returns HTTP 404 Not Found .. : /

EDIT 2 ---

So I just add an new rule to override the content-type:

RewriteRule ^.*/(.*)/create(/.*) includes/function/$1/create.php$2 [T=text/event-stream]

So any file which are named with "create" will be a text/event-stream,

I can't apply it for all php files because I have other files

that shouldn't be a type of text/event-stream, but now I got an new problem..

If my php goes wrong.. It will still respond a text/event-stream for me ..

I'll keep finger it out :D

1

There are 1 best solutions below

0
On

So I got a temporary solution, which is force the content-type to text/event-stream,

just need to add this rule:

RewriteRule ^.*/(.*)/create(/.*) includes/function/$1/create.php$2 [T=text/event-stream]

So any file which are named with "create" will be a text/event-stream,

but it causes another problem: once your PHP error, it'll still return text/event-stream for you.