Forum Moderators: phranque

Message Too Old, No Replies

How can I prevent a .js creating a 404 in .htaccess?

Seems a common issue, but need some help

         

DixonJones

10:21 am on Jan 16, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



When a site calls a .js file on our server, we run code that returns a custom JSON file pre-configured based on the page calling the .js file. So... when a page calls the .js file from a page that we do not have configured JSON for, a 404 gets generated... which we would like to change in .htaccess so that a blank JSON file is returned instead (so redirect to a 200 blank JSON).

Is there an easy way to do that? we don't have a problem is a 404 is NEVER returned, as the domain does not host web pages,

DixonJones

1:02 pm on Jan 16, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Update: We have a solution now... (sorry, I don't have the code to share). I think it remains an issue for a number of people using third party scripts and is well worth checking using a page speed testing tool. Getting that .js into the DOM as fast as possible is definitely something that Google "appreciates"... although I just think Martin wants us to do their hard rendering work for them :)

not2easy

1:04 pm on Jan 16, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



If that is the only 404 error "ever" you could always add this line to .htaccess:
ErrorDocument 404 /404page.html

Substituting "/actual name of blank JSON file" in place of "/404page.html", but be aware that your logs will still show that there was a 404 error because a file was called (requested) that does not exist.

You might get rid of the 404 error itself if you were to create an empty JSON with an alternate name and insert that filename for the situations that generate the error. (Not sure if/how that would be managed?)

DixonJones

2:11 pm on Jan 16, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Thanks!

phranque

8:36 pm on Jan 16, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



which we would like to change in .htaccess so that a blank JSON file is returned instead (so redirect to a 200 blank JSON)

you want an "internal rewrite" to the blank JSON file using mod_rewrite directives.

w3dk

7:54 pm on Jan 18, 2020 (gmt 0)

10+ Year Member Top Contributors Of The Month



...we run code that returns a custom JSON file


Presumably it is your script that returns the 404? (Or not?) I don't see how this is a htaccess/Apache problem, unless there is already some condition in htaccess that determines whether or not to call your script based on the request?