Content-type of application/json

Hello, I am a free hosting customer, and for a project, I need to enable the following: “should serve this file with a content-type of application/json”

So if I upload a .json and share the file link but without the .json extension, the browser returns a web page with the text of the json file.

I have tried uploading a .json but if I enter the address of the uploaded file, without putting the .json extension, I get an error (as if it did not exist).

How can I solve that? Thanks in advance

Welcome to the forum!

If you upload a file named app.json and then navigate to your-website.com/app, the server will try to find a file named “app”. That’s why you are seeing a 404 error.

You can use htaccess to fix this:

RewriteEngine On
Options +MultiViews
RewriteCond §{REQUEST _FILENAME) !-d
RewriteCond {REQUEST FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.json [NC, L]

You should only have RewriteEngine On once in your htacess file, so if you already have it you don’t need to add it again.

4 Likes

Thank you!!!
So I have to create a new htaccess file in the folder where I have those json files, with the code you indicated?

Or what do I have to do is edit the htaccess that already exists in the root directory?

Thank you!!!

Given that the file in the root directory says DO NOT EDIT THIS FILE at the top of it, it should be pretty clear that you should create one in the folder of your website.

7 Likes

Thanks!! :slight_smile:

Hello, I have followed the steps and when I open the json link, this error occurs:

500 Error, please check your php script / enable display_errors in your cpanel

whether I put the link with the .json extension or not.

Thanks in advance, and sorry for so many questions.-(

I saw the .htaccess code sent by @YT_Xaos is incorrect, so I’m gonna correct it:

RewriteEngine On
Options +MultiViews
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.json -f
RewriteRule ^([^\.]+)$ $1.json [NC,L]

Try to replace with this and see if it works! I also did some errors as well, but I fixed those.

6 Likes

I realize that now, I didn’t catch the space between NC, L. Thanks!

3 Likes

Thaaaanks!!! :slight_smile:

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.