0

I am trying to customize all the status codes (error codes) in Apache - Ubuntu 12.04 LTS.

The error message content I get on the browser for the error code 414 is:

Request-URI Too Large

The requested URL's length exceeds the capacity limit for this server.

I went through the file:

/etc/apache2/conf.d/localized-error-pages

to see the default file which shows this error, I found the below line:

ErrorDocument 414 /error/HTTP_REQUEST_URI_TOO_LARGE.html.var

When I go through the file

/usr/share/apache2/error/HTTP_REQUEST_URI_TOO_LARGE.html.var

I didn't find the same message viewed on the browser! How come? Where I can find the default message to edit it? I edited the above file and nothing changed on the browser!

One more thing, how can I customize this error to fit two different websites on the server (each website has different custom_error_414 page)

UPDATE: I removed the comments on the file localized-error-pages but the output didn't change, when I add any path in the file localized-error-pages like:

ErrorDocument 414 /var/www/myproject/error.php 

it doesn't work, but when I add a normal string like:

ErrorDocument 414 "Long Request" 

it does work! Please provide some codes and details on how to fix this issue and how to customize the 414 error for two websites.

UPDATE

Below is the output of wget -O- -S ...

--2014-05-02 21:02:26--  http://localhost/error//HTTP_PRECONDITION_FAILED.html.var
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:80... failed: Connection refused.
Connecting to localhost (localhost)|127.0.0.1|:80... connected.
HTTP request sent, awaiting response...
HTTP/1.1 403 Forbidden
Date: Fri, 02 May 2014 21:02:26 GMT
Server: Apache
Vary: Accept-Encoding
Content-Length: 1
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1

2014-05-02 21:02:26 ERROR 403: Forbidden.

One more thing, I put the error page in /var/www/myproject/error.php but when I tried:

ErrorDocument 414 /var/www/myproject/error.php 

It didn't work :( While

ErrorDocument 414 "Sorry, request is too long"

Worked out!

How come? Why it doesn't redirect me to the error page?

One more thing, I need to make for two websites two different pages, how can I achieve:

ErrorDocument 414 sites1 ==> /var/www/myproject1/error.php
ErrorDocument 414 sites2 ==> /var/www/myproject2/error.php

I want to put the error pages on my /var/www/project folder

I would greatly appreciate if you can send me direct steps to follow as I am not experienced system admin and I can't find documentation about this on the internet, it is very poor.

EDIT

Thanks for the detailed answer, I have followed your steps but I have minor issues would appreciate your input.

a. Below is the configuration file for the errors /etc/apache2/conf.d/localized-error-pages




    
      
        
            #Alias /error/ "/usr/share/apache2/error/"
            
                Options IncludesNoExec
                AddOutputFilter Includes html
                AddHandler type-map var
                Order allow,deny
                Allow from all
                LanguagePriority en cs de es fr it nl sv pt-br ro
                ForceLanguagePriority Prefer Fallback
            
            ErrorDocument 404 /error/HTTP_NOT_FOUND.html.var
            ErrorDocument 405 /error/HTTP_METHOD_NOT_ALLOWED.html.vr
            ErrorDocument 408 /error/HTTP_REQUEST_TIME_OUT.html.var
            ErrorDocument 410 /error/HTTP_GONE.html.var
            ErrorDocument 411 /error/HTTP_LENGTH_REQUIRED.html.var
            ErrorDocument 412 /error/HTTP_PRECONDITION_FAILED.html.var
            ErrorDocument 414 /error/HTTP_REQUEST_URI_TOO_LARGE.html.var
        
      
    

b. I have enabled the 3 mods successfully.

c. I have copied:

cp -R /usr/share/apache2/error/include /usr/share/apache2/include_site1
also
cp -R /usr/share/apache2/error/include /usr/share/apache2/include_site2


d. In site1 configuration in the page: /etc/apache2/sites-available/site1, I added:

Alias /error/include "/usr/share/apache2/error/include_site1"
Alias /error/ "/usr/share/apache2/error/"

e. apache2ctl configtest Syntax OK

f. Reloaded the Apache, loaded without errors.

g. wget -O- ..../error/HTTP_REQUEST_URI_TOO_LARGE.html.var result is:

HTTP request sent, awaiting response... 200 OK Length: 223 [text/html] Saving to: `STDOUT'

0% [ ] 0 --.-K/s [an error occurred while processing this directive]

The length of the requested URL exceeds the capacity limit for
    this server. The request cannot be processed.

[an error occurred while processing this directive] 100%[==============================================================================================================================>] 223 --.-K/s in 0s

2014-05-04 17:48:33 (73.1 MB/s) - written to stdout [223/223]



h. Tried from the browser: .../error/HTTP_REQUEST_URI_TOO_LARGE.html.var The same result as step 8 but the in the browser


i. When I try placing huge request in the browser like: ...../site1/sj7xlekrjsuglkehgigl.........,
I get empty output on the browser and in the network (F12) I can see 414 error status

How can I get this working? I need to give the code 404 for the 414 error and I need to use (include) the customized PHP pages on each website, which is /var/www/site1/not_found.php

Edit 2

I have added the below in sites available, but if I put long URL in browser, it doesn't redirect me to index.php nor to not_found.php page. I need the long URL error (414) to direct me to 404 page :( I am really upset I can't find any help online. Error like 414 doesn't work like 404 easily, they built inside the system.

404 is very easy for me! But can't configure 414!!!

 ErrorDocument 404 /not_found.php
<IfModule mod_rewrite.c>
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /index.php [L]
</IfModule>

For example in sites-available the below doesn't work ErrorDocument 414 /not_found.php

1 Answer 1

0

Localized error messages are commented out in the default configuration. You need to modify the localized-error-pages.conf file to enable the localized pages. It is dependent on a number of modules being enabled.

If you are getting the files from the error directory, these files contain the response in multiple languages. The content is determined based on the language(s) specified by the browser. The header and footer are found in the include sub-directory.

Some browsers may use their own message in place of the server provided content. Try using wget, curl, or w3m to test the output. Once you are getting a correct message with one of them try using your browser. Your browser may also have cached a response from a previous attempt, so you may need to force a refresh or flush the browser cache.

You can use different files for different sites or use server side includes to add site specific information.

The URL for the error pages should be in the form http://www.example.com/error/HTTP_NOT_FOUND.html.

EDIT: I am working from the install on Ubuntu 13.10 but the configuration has been quite stable over the years. The provided pages require the alias, include, and negotiation. I believe you need a restart to enable them if they are not already enabled. The a2enmod command should tell you if they are already enabled.

Try using wget -O- -S http://localhost/error//HTTP_PRECONDITION_FAILED.html.var to verify the page can be served. Until that works, the error page will not be served. Then try using w3m http://localhost/failing_URL to test the output. The error pages don't seem to be served if the client doesn't provide the language negotiation information.

The directory /usr/share/apache2/error/include should have the header and footer for the message. You can use Alias directives to change the directory these files are read from to customize by site. You need to ensure this directive occurs before the Alias directive for the /error directory. Doing the aliasing in the site configuration would be appropriate. Something like:

Alias /error/include/ "/var/www/site/error/include/"
Alias /error/ "/usr/share/apache2/error/"

You may want to verify your configuration with apache2ctl configtest to see if you get any warnings. Check the /var/log/apache2/errorlog file for related messages. If you have it enabled, the sever-info page is also helpful in diagnosing what is happening.

EDIT: This is an extracts of my localized-error-pages configuration with several error documents omitted. This can likely be common for both sites as the content you need to modify should be in the /errors/include

<IfModule mod_negotiation.c>
  <IfModule mod_include.c>
    <IfModule mod_alias.c>
        #Alias /error/ "/usr/share/apache2/error/"
        <Directory "/usr/share/apache2/error">
            Options IncludesNoExec
            AddOutputFilter Includes html
            AddHandler type-map var
            Order allow,deny
            Allow from all
            LanguagePriority en cs de es fr it nl sv pt-br ro

            ForceLanguagePriority Prefer Fallback
        </Directory>
        ErrorDocument 404 /error/HTTP_NOT_FOUND.html.var
        ErrorDocument 405 /error/HTTP_METHOD_NOT_ALLOWED.html.vr
        ErrorDocument 408 /error/HTTP_REQUEST_TIME_OUT.html.var
        ErrorDocument 410 /error/HTTP_GONE.html.var
        ErrorDocument 411 /error/HTTP_LENGTH_REQUIRED.html.var
        ErrorDocument 412 /error/HTTP_PRECONDITION_FAILED.html.var
    </IfModule>
  </IfModule>
</IfModule>:

Verify the required modules are loaded with the command:

sudo a2enmod negotiation include alias

If should tell you if if loaded any modules in which case you will need to restart apache.

Copy the error/include sub-directory for each of the sites. Edit the files in these files to modify the error pages.

 sudo cp -R /usr/share/apache2/error/include /usr/share/apache2/include_site1
 sudo cp -R /usr/share/apache2/error/include /usr/share/apache2/include_site2

Add the following lines to the site configurations (change site1 to site2 for the second site):

Alias /error/include "/usr/share/apache2/error/include_site1"
Alias /error/ "/usr/share/apache2/error/"

Check the configuration and reload apache:

sudo apache2ctl configtest
sudo apache2ctl reload

Verify the pages load. If these fail, you need to resolve this issue.

wget -O- http://site1/error/HTTP_PRECONDITION_FAILED.html.var
wget -O- http://site2/error/HTTP_PRECONDITION_FAILED.html.var

Try locally using w3m (text browser) as wget does not get served the error page.

w3m http://site1/nopage
w2m http://site2/nopage

If this works try with a browser. You may want to try setting your preferred language to something other than English to see how the language variance works.

EDIT2: You can handle the errors in your php index page like WordPress does. This is done with rewrite rules to pass any missing URLs to the index page. (You need to handle the 404 condition in the index page.) I haven't tested any cases not generated within WordPress.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

If you want to use your own PHP error pages skip the all above steps and just add error document settings in your pages. It appears you just want to direct apache2 to use you pages, not get it to sever the apache2 custom pages. (You can use the same document for multiple errors.) Add lines like this to your site specification.

ErrorDocument 404 /not_found.php

You can read and set the status of the page. (Set the status before outputting anything else as you need to set the status before the headers are written.) From the PHP documentation on http response code:

<?php
 http_response_code(404);
?>
9
  • thanks but the page is localized-error-pages, not localized-error-pages.conf. Please see my update on the question. Also, more than one browser having same message. Would you please give me details and show some codes on how to have to different customized pages for 2 websites? I am really lost and it doesn't work!
    – moderns
    Apr 29, 2014 at 10:30
  • @moderns I've updated with more information. Work step by step, get the pages working as pages, then move on to getting them displayed. Read the comments in the the localized-error-pages configuration file.
    – BillThor
    Apr 29, 2014 at 12:51
  • thank you for the details but honestly I couldn't manage that as I need specific steps to follow and you were explaining in advance level (generalize). Kindly check my latest update and would appreciate if you provide me with direct clear steps to follow. Thank you.
    – moderns
    May 2, 2014 at 21:26
  • @moderns I've added some examples and instructions. Good luck. curl will retrieve the custom error pages.
    – BillThor
    May 4, 2014 at 0:41
  • Thank you very much for the details. I followed your hints but still I need some input from you to solve this issue. Kindly check my new update. Thank you!
    – moderns
    May 4, 2014 at 21:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .