Home > front end >  Regrouping errorDocument status codes into one - Possible?
Regrouping errorDocument status codes into one - Possible?

Time:09-17

I wanted to simplify my htaccess errorDocument section in one line, is it possible? Can't find any documentations on this on the web. Thanks in advance!

Actual htaccess:

ErrorDocument 400 /errors.php?error=400
ErrorDocument 401 /errors.php?error=401
ErrorDocument 402 /errors.php?error=402
ErrorDocument 403 /errors.php?error=403
ErrorDocument 404 /errors.php?error=404
ErrorDocument 405 /errors.php?error=405
ErrorDocument 406 /errors.php?error=406
ErrorDocument 407 /errors.php?error=407
ErrorDocument 408 /errors.php?error=408
ErrorDocument 409 /errors.php?error=409
ErrorDocument 410 /errors.php?error=410
ErrorDocument 411 /errors.php?error=411
ErrorDocument 412 /errors.php?error=412
ErrorDocument 413 /errors.php?error=413
ErrorDocument 414 /errors.php?error=414
ErrorDocument 415 /errors.php?error=415
ErrorDocument 416 /errors.php?error=416
ErrorDocument 417 /errors.php?error=417
ErrorDocument 418 /errors.php?error=418
ErrorDocument 421 /errors.php?error=421
ErrorDocument 422 /errors.php?error=422
ErrorDocument 423 /errors.php?error=423
ErrorDocument 424 /errors.php?error=424
ErrorDocument 425 /errors.php?error=425
ErrorDocument 426 /errors.php?error=426
ErrorDocument 428 /errors.php?error=428
ErrorDocument 429 /errors.php?error=429
ErrorDocument 431 /errors.php?error=431
ErrorDocument 451 /errors.php?error=451

ErrorDocument 500 /errors.php?error=500
ErrorDocument 501 /errors.php?error=501
ErrorDocument 502 /errors.php?error=502
ErrorDocument 503 /errors.php?error=503
ErrorDocument 504 /errors.php?error=504
ErrorDocument 505 /errors.php?error=505
ErrorDocument 506 /errors.php?error=506
ErrorDocument 507 /errors.php?error=507
ErrorDocument 508 /errors.php?error=508
ErrorDocument 510 /errors.php?error=510
ErrorDocument 511 /errors.php?error=511

CodePudding user response:

Impossible.

you should handle each error code. No option to manage it with regex or something like that.

CodePudding user response:

You'll need a separate directive for each error code, however:

  • You don't need to catch all these error states since most of these won't trigger your script anyway as most of them are triggered too early, before .htaccess is processed (to run your ErrorDocument directive).

  • You don't need to explicitly pass the error code in the form of the error URL parameter since this error code is already available to your PHP script in the form of the $_SERVER['REDIRECT_STATUS'] superglobal.

    So, the following is sufficient:

    ErrorDocument 404 /errors.php
    

    And then reference $_SERVER['REDIRECT_STATUS'] to get the response code, eg. "404" in this instance.

  • Related